-
-
Notifications
You must be signed in to change notification settings - Fork 337
LONDON | MAY2025 | STRUCTURING AND TESTING DATA | SPRING 2 JESUS DEL MORAL #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,28 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| // declare str again inside the function, which causes a syntax error | ||
|
|
||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // str[0].toUpperCase() → capitalizes the first letter. | ||
|
|
||
| // str.slice(1) → returns the rest of the string starting from index 1. | ||
|
|
||
| // Combines both parts into a new string. | ||
|
|
||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function capitalise(str) { | ||
| let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitalised; | ||
| } | ||
|
|
||
| console.log(capitalise("hello")); // "Hello" | ||
| console.log(capitalise("jesus")); // "Jesus" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // We are not calling to the function from console.log, the function is convertToPercentage | ||
| // Also, its using decimalNumber outside the function, decimalnumber is a parameter it’s not defined in the global scope. | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| console.log(convertToPercentage(0.5)); // "50%" | ||
| console.log(convertToPercentage(0.25)); // "25%" | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // The function should return a variable, the current function doesn't return anything, just console.log() | ||
|
|
||
| // =============> write your prediction here | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| return(a * b); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is correct |
||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| // Predict and explain first... | ||
|
|
||
| //The function is not returning anything | ||
|
|
||
| // =============> write your prediction here | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| return(a+b) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is correct. In JS lines can be terminated with a semicolon. |
||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
|
|
||
| // Will be an error because we are not passing a parameter in the function getLastDigit | ||
| // the value of num is taking always the value 103, it declarate at the beginning of the program | ||
|
|
||
| // =============> Write your prediction here | ||
|
|
||
| const num = 103; | ||
| // const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| function getLastDigit(num) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is a good parameter name. |
||
| return num.toString().slice(-1); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,11 @@ | |
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| const bmi = 0 | ||
|
|
||
| height = height * height; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this solution is correct, perhaps in the future we can avoid reusing variables? It makes the code more confusing, it is good practice to use new variables: |
||
| return(weight / height).toFixed(1); | ||
| } | ||
|
|
||
|
|
||
| console.log(calculateBMI(72, 1.80)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,13 @@ | |
| // You will need to come up with an appropriate name for the function | ||
| // Use the MDN string documentation to help you find a solution | ||
| // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase | ||
|
|
||
| function toUpperSnakeCase(str) { | ||
| return str | ||
| .trim() // Remove leading/trailing whitespace | ||
| .toUpperCase() // Convert to uppercase | ||
| .replace(/\s+/g, '_'); // Replace spaces with underscores | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a nice use of regular expressions here and I like your helpful comments. Well done. |
||
| } | ||
|
|
||
| console.log(toUpperSnakeCase('hello world')); | ||
| console.log(toUpperSnakeCase('lord of the rings')); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,32 @@ | |
| // You will need to declare a function called toPounds with an appropriately named parameter. | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
|
|
||
|
|
||
|
|
||
|
|
||
| function toPounds(penceString){ | ||
|
|
||
|
|
||
| const penceStringWithoutTrailingP = penceString.substring( | ||
| 0, | ||
| penceString.length - 1 | ||
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
|
|
||
| console.log(`£${pounds}.${pence}`); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function behaves as expected. Are we supposed to use a return statement instead of a console.log command? We have a console.log command on line 33 as well. |
||
|
|
||
| } | ||
|
|
||
| console.log(toPounds('400p')); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,24 +11,26 @@ function formatTimeDisplay(seconds) { | |
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
|
|
||
| console.log(formatTimeDisplay(61)); | ||
|
|
||
| // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit | ||
| // to help you answer these questions | ||
|
|
||
| // Questions | ||
|
|
||
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
| // Pad function is called 3 times | ||
|
|
||
| // Call formatTimeDisplay with an input of 61, now answer the following: | ||
|
|
||
| // b) What is the value assigned to num when pad is called for the first time? | ||
| // =============> write your answer here | ||
| // The value is 0 | ||
|
|
||
| // c) What is the return value of pad is called for the first time? | ||
| // =============> write your answer here | ||
| // The value is 00 | ||
|
|
||
| // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // The value assigned last time we called Pad is 1, but the return is 01 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These answers are logically correct. Be mindful of what the question is asking here, We can rephrase the last part of the question as: The return value of pad is not being asked for here. |
||
|
|
||
| // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // The value is returned is 01 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explain questions ask for verbal reasoning for the reason that the return value is what it is. Your answer could be something along the lines of: |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me.