-
-
Notifications
You must be signed in to change notification settings - Fork 337
Manchester | 26-ITP-Jan | Mehroz Munir | Sprint 2 | Coursework #1077
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
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 | ||
| // I am not able to predict any error, everything seems fine to me. | ||
|
|
||
| // 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; | ||
| } | ||
| }*/ | ||
|
|
||
| // =============> Yeah, str name for the variable is already being used as the parameter of the function, | ||
| // that's why we can't declare it agian as being done in line 8. | ||
| // =============> the correct code would be as follows: | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| let capitaliseStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitaliseStr; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,25 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
||
| // =============> There would be a syntax error because the decimalNumber is declared again as const | ||
| //and logically there is no need for initializing decimalNumber again with 0.5 value as in that case the method would return 50% always | ||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| /*function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| }*/ | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> Yes, the error is because the identifier decimalNumber has already been declared. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
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. Finally, you need to correct the code to fix the problem. Correct the code below. Code seems to be commented out |
||
| // =============> write your new code here | ||
| // =============> the correct code would be as follows:- | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,29 @@ | |
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // =============> In the parameters of function we can write the name of the variable but not the actual value. | ||
| //we write actual value when we call the function. | ||
|
|
||
| function square(3) { | ||
| /*function square(3) { | ||
| return num * num; | ||
| } | ||
| }*/ | ||
|
|
||
| // =============> write the error message here | ||
| /* | ||
| function square(3) { | ||
| ^ | ||
|
|
||
| SyntaxError: Unexpected number | ||
| */ | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> Yeah, the error is because that when writing the function it does not expect number value instead of the parameter name | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
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. same here
Author
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. Hi @netEmmanuel |
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| //I think that the problem is our function multiply does not return any value but write in console | ||
| // However, we are calling the function inside another console.log and expecting the function to return some value. | ||
|
|
||
| function multiply(a, b) { | ||
| /*function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| */ | ||
| // =============> Yeah, when we run the code we get the following output which shows that second console is not able to get the value from the function as the function does not return any value. | ||
| /* | ||
| 320 | ||
| The result of multiplying 10 and 32 is undefined | ||
| */ | ||
|
|
||
| // 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,20 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| function sum(a, b) { | ||
| // In the below function, after return there is a semi colon which syntactically would just return nothing and any line of code below that would not run | ||
| /*function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
| }*/ | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| //The function is returning undefined | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
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. The code is commented out. You need to uncomment this part of the code, which is your fix for the error above |
||
|
|
||
| function sum(a, b) { | ||
| return a + b; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,38 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
|
|
||
| /* The last digit of 42 is 2 | ||
| The last digit of 105 is 5 | ||
| The last digit of 806 is 6 | ||
| */ | ||
| const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| /*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)}`); | ||
| */ | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> 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 | ||
| // =============> I just realized now that the function getLastDigit is just always just returning the last digit of cons num | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| 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)}`); |
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.
There's an error in the function. Have a look again and write the fix in the commented out code below on line 16