-
-
Notifications
You must be signed in to change notification settings - Fork 390
West Midlands | May 2026 | Muhammad-Burhan Mustafa | Sprint 2 | Module-Structuring-and-Testing-Data #1443
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?
West Midlands | May 2026 | Muhammad-Burhan Mustafa | Sprint 2 | Module-Structuring-and-Testing-Data #1443
Changes from all commits
ce7f3df
509aee5
1f4dc42
514c086
2905703
e52b096
329448e
25fffb6
fa080a4
6a4c5ad
5dc9a63
f9f9cdd
fa6a305
77770dd
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,26 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here | ||
| // An error will be thrown because str has already been declared as the function's parameter. | ||
| // The function parameter and the let variable are both declared in the same function scope. | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
| // 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 | ||
| // The error occurs because str is already declared as the function's parameter, so it cannot be | ||
| // declared again using let inside the same scope. By changing the new variable's name to | ||
| // capitalisedStr, there is no longer a naming conflict and the code runs without any errors. | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function capitalise(str) { | ||
| let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitalisedStr; | ||
| } | ||
|
|
||
| console.log(capitalise("mmm")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,25 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // =============> write your prediction of the error here: | ||
| // we have used a number instead of naming the parameter so we should get a syntax error | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // JavaScript read the number 3 as a number rather than the name of the parameter. | ||
| // Parameters must have valid names | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // Since line 7 uses a const variable that can not be changed I would expect the last digit to be remain three since num has been assigned to be 103 | ||
|
|
||
| const num = 103; | ||
|
|
||
|
|
@@ -14,11 +15,30 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`); | |
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| // =============> write the output here: | ||
| // The last digit of 42 is 3 | ||
| // The last digit of 105 is 3 | ||
| // The last digit of 806 is 3 | ||
|
|
||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // // Line 7 uses the const variable num which has been assigned the value 103 and the last digit of that number is 3. When the function getLastDigit is called it | ||
|
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. Would the behaviour be different if it were a |
||
| // returns the last digit of num. Regardless of what number we use when calling the function it will always return 3 because the function has no parameter to read | ||
| // the arguments and always uses the value stored inside num instead. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function getLastDigit(num) { | ||
| return num.toString().slice(-1); | ||
| } | ||
|
|
||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
|
|
||
| // getLastDigit is not working properly because the function has no parameter to read the numbers used when it is called upon. Instead it always uses the const variable | ||
| // num which has been assigned the value 103 so it will always return the last digit of 3. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,9 @@ | |
| // 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 upperSnakeCase(string) { | ||
| return string.toUpperCase().replaceAll(" ", "_"); | ||
| } | ||
|
|
||
| console.log(upperSnakeCase("i love cyf you guys are awesome!")); | ||
|
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. 😄 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,25 @@ | |
| // 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) { | ||
|
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. In JavaScript, standard variables and functions should be named with |
||
| 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"); | ||
|
|
||
| return console.log(`£${pounds}.${pence}`); | ||
| } | ||
|
|
||
| penceToPounds("399p"); | ||
| penceToPounds("580990p"); | ||
| penceToPounds("489302849032p"); | ||
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.
What's another problem with the original code?