-
-
Notifications
You must be signed in to change notification settings - Fork 336
London | 26-ITP-January | Damian Dunkley | Sprint 2 | coursework/sprint-2 #1012
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
base: main
Are you sure you want to change the base?
Changes from all commits
a42a22b
074adf1
b50c220
b5f1e22
863b7a8
71ca135
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,19 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| //Will error because we are trying to declare a variable with the same name as the parameter of the function. The error message will likely indicate that there is a syntax error or that the variable 'str' has already been declared. | ||
| // 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; | ||
| } | ||
| //function capitalise(str) { | ||
| //let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| //return str; | ||
| //} | ||
|
|
||
| // =============> write your explanation here | ||
| // The error is occurring because we are trying to declare a variable 'str' inside the function that has the same name as the parameter 'str'.To fix this problem, create an variable with a new name Newstr . So we would change the line to: Newstr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
|
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. Well done. |
||
| let newStr = `${str[0].toUpperCase()}${str.slice(1)}`; //change the variable name to newStr to conform with camelCase name convention. | ||
| return newStr; | ||
| } | ||
| console.log(capitalise("hello world")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,27 @@ | |
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
||
| // The error will occur because we are trying to declare a variable with the same name as the parameter of the function. The error message will likely indicate that there is a syntax error or that the variable 'str' has already been declared. | ||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| //function convertToPercentage(decimalNumber) { | ||
| //const decimalNumber = 0.5; | ||
| //const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| //return percentage; | ||
| //} | ||
|
|
||
| console.log(decimalNumber); | ||
| //console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // The error is occurring because we are trying to declare a variable 'decimalNumber' inside the function that has the same name as the parameter 'decimalNumber'. To fix this problem, we can different variable name for the parameter or the variable inside the function. For example, we could change the line to: const decimalNum = 0.5; and then use 'decimalNum' instead of 'decimalNumber' in the rest of the function. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNum = 0.5; | ||
|
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. You successfully sorted the redeclaration error. |
||
| const percentage = `${decimalNum * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.5)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,20 @@ | |
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| //function square(3) { | ||
| // return num * num; | ||
| //} | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // SyntaxError: Unexpected number '3' | ||
| // =============> explain this error message here | ||
| // The error message is indicating that there is a syntax error in the code because we are trying to use a number '3' as a parameter name in the function declaration, which is not allowed in JavaScript. Parameter names must be valid identifiers, which cannot start with a number. To fix this error, we need to change the parameter name to a valid identifier, such as 'num' or 'x'. So we would change the line to: function square(num) { return num * num; } | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function square(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. This is well done. |
||
| return num * num; | ||
| } | ||
| console.log(square(3)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // The error will occur because the function 'sum' is not returning the result of multiplying 'a' and 'b'. Instead, it is returning 'undefined' due to the function being incomplete. The error message will likely indicate that the result of the sum is 'undefined' when we try to log it to the console. | ||
| //function multiply(a, b) { | ||
| //console.log(a * b); | ||
| //} | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| //console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // The error is occurring because the function 'multiply' is not returning any value. In JavaScript, if a function does not explicitly return a value, it returns 'undefined' by default. To fix this problem, we need to add a return statement to the function that returns the result of multiplying 'a' and 'b'. For example, we could change the line to: return a * b; so that the function will return the correct result when called. | ||
|
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. Good. |
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| //The error will occur because the function 'sum' is not returning the result of summing 'a' and 'b'. Instead, it is returning 'undefined' due to the function being incomplete. The error message will likely indicate that the result of the sum is 'undefined' when we try to log it to the console. | ||
| //function sum(a, b) { | ||
| //return; | ||
| //a + b; | ||
| //} | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| //console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // The error is occurring because the function 'sum' is not returning any value. In JavaScript, if a function does not explicitly return a value, it returns 'undefined' by default. To fix this problem, we need to add a return statement to the function that returns the result of summing 'a' and 'b'. For example, we could change the line to: return a + b; so that the function will return the correct result when called. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function sum(a, b) { | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,27 @@ | |
| // 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"); | ||
|
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 good. |
||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
|
|
||
| return `£${pounds}.${pence}`; | ||
| } | ||
|
|
||
| console.log(toPounds("123p")); // should return "£1.23" | ||
| console.log(toPounds("5p")); // should return "£0.05" | ||
| console.log(toPounds("75p")); // should return "£0.75" | ||
| console.log(toPounds("1000p")); // should return "£10.00" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,10 @@ function formatTimeDisplay(seconds) { | |
|
|
||
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
|
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. Well done. |
||
| let num = 61; | ||
| console.log(formatTimeDisplay(num)); // should return "00:01:01" | ||
|
|
||
|
|
||
|
|
||
| // 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 | ||
|
|
@@ -18,17 +22,18 @@ function formatTimeDisplay(seconds) { | |
|
|
||
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
|
|
||
| // The function pad will be called three times when formatTimeDisplay is called, once for each of; hours, minutes, and seconds.These are then passed as an argument to the pad function to ensure that they are displayed with two digits in the final output string. | ||
| // 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 assigned to num when pad is called for the first time is the totalHours value, which is 0. The value of 61 seconds is only one minute and one second, any value that includes hours must be in excess of 3600 seconds. | ||
| // c) What is the return value of pad is called for the first time? | ||
| // =============> write your answer here | ||
|
|
||
| // The return value of pad when it is called for the first time is "00". This is because the totalHours value is 0, and when it is converted to a string and passed to the pad function, it will be padded with a leading zero to ensure that it has two digits, resulting in "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 to num when pad is called for the last time in this program is the remainingSeconds value, which is 1. This is because the input of 61 seconds results in 1 second remaining after accounting for the full minute. | ||
| // 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 | ||
| //This value displayed with two digits in the final output string, resulting in "01". | ||
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 is good.