-
-
Notifications
You must be signed in to change notification settings - Fork 332
London | 26-ITP-Jan | Angela McLeary | Sprint 2 | Coursework/sprint 2 #1110
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
6b40bf3
7241ee3
b3de302
ed06a0f
3a3fa15
d7ff511
ca5d386
e91663e
c9f66f9
00271f5
e1876d6
ed21c08
e0a3306
78d96f8
77f2ef2
0d05247
20a496c
82fccd4
61a12b3
364b18d
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,44 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // 1.*Answer | ||
| // We have declared the "str" twice.It is already declared | ||
| // in this function's parameter, hence causing the conflict. | ||
| // Although "let" allows re-assignment of variables, the issue is redeclaration, | ||
| // not reassignment. | ||
|
|
||
|
|
||
| // call the function capitalise with a string input | ||
| // 2.*Answer | ||
| // It throws an error detailed below; | ||
| // / home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/tempCodeRunnerFile.js:2 | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // ^ | ||
| // SyntaxError: Identifier 'str' has already been declared. | ||
|
|
||
|
|
||
| // interpret the error message and figure out why an error is occurring | ||
| // 3.*Answer | ||
| // The SyntaxError - Something is invalid that is not following | ||
| // Javascript rules. | ||
| // The identifier - a name used in the code has a problem and it | ||
| // lets m know which one in this case "str". It also let's me know | ||
| // what the problem is in this case "str" has already been declared. | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
| //Fix this code: | ||
| // function capitalise(str) { | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| // } | ||
|
|
||
| // =============> write your explanation here | ||
| //I have re-assigned the "str" variable and it now runs with no errors. | ||
| // I also checked with console.log to see the output. | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function capitalise(str) { | ||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
| console.log(capitalise("justice")); | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,36 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // 1.*Answer | ||
| // We have declared "decimalNumber" twice, which is not allowed. | ||
| // It is already declared as a function parameter, causing a conflict. | ||
| // console.log(decimalNumber) results in an error because it is outside | ||
| // the function scope.It should call console.log(convertToPercentage(decimalNumber)); | ||
|
|
||
| // 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; | ||
| } | ||
| //Try playing computer with the example to work out what is going on | ||
| // function convertToPercentage(decimalNumber) { | ||
| // const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| console.log(decimalNumber); | ||
| // return percentage; | ||
| // } | ||
| // console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // 2.*Answer | ||
| // It throws an error detailed below; | ||
| // /home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/1.js:12 | ||
| // const decimalNumber = 0.5; | ||
| // ^ | ||
| // SyntaxError: Identifier 'decimalNumber' has already been declared | ||
|
|
||
| // Removed the const declaration and returned the percentage calculation | ||
| // using a template literal to append the percent symbol. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| return `${decimalNumber * 100}%`; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.5));//returns 50% |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,34 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
| // 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 | ||
| // /home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/2.js:8 | ||
| // function square(3) { | ||
| // ^ | ||
| // The SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
|
|
||
| // This occurs because the number "3" is used as a function parameter. | ||
| // This is not valid syntax. A function parameter must be a valid variable name (an identifier), | ||
| // not a number. JavaScript expects an identifier or no parameter at all. | ||
| // That's why it throws a syntax error. | ||
| // | ||
| // 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,30 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // The first console.log prints 320 | ||
| //It throws an error at console.log. | ||
| // =============> write your prediction here | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| // 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 | ||
| // It return the following output: | ||
| // 320 | ||
| // The result of multiplying 10 and 32 is undefined | ||
|
|
||
| // why? | ||
| //the computer reads the values in the second console.log "multiply(10, 32)" and | ||
| //the first console.log only prints the value, it does not store it. | ||
| //the second console.log outputs the template literals and undefined | ||
| // as it has no value to reach. | ||
|
|
||
|
|
||
| // 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)}`); | ||
| //prints: The result of multiplying 10 and 32 is 320 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,24 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
| // The computer reads the return statement and stops executing the function. | ||
| // So the function stops running before it reaches a + b. | ||
| // Because of that, nothing is returned, | ||
| // and the console.log prints the text and the values are undefined. | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| // current output:The sum of 10 and 32 is undefined | ||
|
|
||
| // =============> write your explanation here | ||
| // Fix the code to make it work: | ||
| // function sum(a, b) { | ||
| // return a + b; | ||
| // } | ||
| // console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| //To make it work, I need to return a + b. | ||
| // 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,54 @@ | ||
| // Predict and explain first... | ||
| // The function does not take in any parameters, therefore it | ||
| // refers to the global const variable = 103 | ||
| // So every console.log will print 3 every time. This function is not | ||
| // re-useable as it stands. | ||
|
Comment on lines
+4
to
+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. Good point and observation. This function is not re-useable as it stands, functions should ideally be self-contained and reusable.
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. @tee4tao |
||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| //output is 3 for every console.log. | ||
|
|
||
| const num = 103; | ||
| // const num = 103; | ||
|
|
||
| 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 | ||
| // "/home/justice/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/2-mandatory-debug/2.js" | ||
| // 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 | ||
| // The function refers to the global const variable = 103 | ||
| // So every console.log returns 3 every time. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| const num = 103; | ||
|
|
||
| function getLastDigit(num) { | ||
| return num.toString().slice(-1); | ||
| } | ||
|
|
||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| // prints: The last digit of 42 is 2 | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| //prints: The last digit of 105 is 5 | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
| //prints: The last digit of 806 is 6 | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| // now the code works correctly | ||
|
|
||
| // I declared "num" as a parameter of the function, | ||
| // so the function now uses the value passed into it | ||
| // instead of the global variable "num". | ||
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.
In your explanation, you mentioned that the second console.log outputs undefined 'as it has no value to reach.' You are exactly right! The technical term for this in JavaScript is an implicit return. If you don't explicitly tell a function what to return, JavaScript automatically forces it to return undefined behind the scenes. Keep up the great work!
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.
@tee4tao Thank you for the feedback. I will note that for future explanations.
Thank you
Angela.