Repository navigation
London | 26-ITP-Sep | Abdennour Hachemi | Sprint 3 | Coursework3 #1591
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 20 commits
00fdc77
567077e
c91ef99
0afe2a9
292e899
c7c8f3a
a239738
63cc7a9
c1fab78
ca30eb5
142dcbd
5a1f129
f95f159
f598953
f4b5d6a
cf05ff4
7947510
b503934
92e3132
514bece
760800c
a1cc998
5698bf5
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 | ||
| // Tha parameter 'str' is declared another time insie the function , this will cause an error to be thrown. | ||
|
|
||
| // 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 SyntaxError: Identifier 'str' has already been declared => line 10 varibale str should not be declared again. | ||
| // =============> write your new code | ||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| console.log(capitalise("hello world")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,22 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here D: Answer => variable decimalNumber is declared twice : one time in the parameter and another time inside the function | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
| // function convertToPercentage(decimalNumber) { | ||
| // //const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
| // return percentage; | ||
| // } | ||
|
|
||
| // console.log(decimalNumber); | ||
| // =============> write your explanation here | ||
| // solution :Also in line 15 console.log is trying print a variable which is not defined instead of calling the function convertToPercentage . | ||
| // 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 | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| console.log(convertToPercentage(0.5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,21 @@ | ||
|
|
||
| // 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 : The funcation parameter 3 this will cause an error to be thrown because the parameter is not a valid identifier. | ||
| // function square(3) { | ||
| // return num * num; | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // } | ||
|
|
||
| // =============> write the error message here | ||
| // =============> write the error message here: SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> explain this error message here: SyntaxError: Unexpected number , the parameter 3 is not a valid identifier it should ba valid identifier like num. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> write your prediction here : The function multipy does not return any value so the result of calling the function will be unknown. | ||
|
|
||
| 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 | ||
| // =============> write your explanation here : Calling the function has logged the result in the console but it has not returned the result ot the multiplicaion so the second console.log outside the function has no value to print and has printed 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,18 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here: this code will give an error because the funcion does not return any value and calling it within the consle.log will print undefined. | ||
|
|
||
| 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 | ||
| // =============> write your explanation here : the sum a+b in line 6 should precede the return statement inorder to return the value. | ||
| // 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 |
|---|---|---|
| @@ -1,24 +1,35 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> Write your prediction here: This code will throw an error because the function getLastDigit does not take any parameters but we are passing a parameter to it in the console.log statements. | ||
|
|
||
| const num = 103; | ||
|
Contributor
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 said this yourself on line 35:
But the line is still there. The CYF style guide says the same thing. You ticked it in your PR checklist ("My changes follow the style guide"). Under "Don't leave unused variables" it says:
https://curriculum.codeyourfuture.io/guides/reviewing/style-guide/#dont-leave-unused-variables Please delete line 6. Then run
Contributor
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. It is in comments now, with the rest of the original code. |
||
|
|
||
| function getLastDigit() { | ||
| return num.toString().slice(-1); | ||
| } | ||
| // function getLastDigit() { | ||
| // 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)}`); | ||
| // 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 | ||
| // =============> write the output here :output | ||
| /* 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 | ||
| // =============> write your explanation here: The funtion return one single value (3) no matter what parameter is passed to it because the function does not take any parameters and it is using the gobal variable num which is set to 103. So the last digit of 103 is always 3. | ||
| // 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 | ||
| // Explain why getLastDigit is not working properly - correct the problem : The first declaration of num as constent should be removed and the function getLastDigit should take a parameter num to return the last digit of the number passed to it. | ||
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.
Your new code on lines 15 to 19 is in comments. So it never runs. The original code on lines 8 to 11 still runs. Run this file with node. What do you see? The broken code should stop running, and your fix should run. Can you swap them?
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.
Fixed now. Your fix runs, and the original is in comments. Good.