-
-
Notifications
You must be signed in to change notification settings - Fork 337
Sheffield | 26-ITP-jan | Richard Frimpong | Sprint 2 | Structuring and Testing Data #1125
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 16 commits
f33028b
4e7c018
25a5763
d2582ca
29a57ec
b9fcbcc
4360d58
58789cd
e795cdd
d1865f3
cb75590
4136e27
400e5a8
5026ab3
f9b067e
27a5872
e9feff8
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,20 +1,29 @@ | ||
|
|
||
| // 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 | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // function square(3) { | ||
| // return num * num; | ||
| // } | ||
|
|
||
| // =============> write the error message here | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // The function parameter is written as 3, which is a number literal. | ||
| // Function parameters must be variable names (identifiers). | ||
| // JavaScript does not allow numbers as parameter names, | ||
| // so it throws a SyntaxError before running the program. | ||
|
|
||
| // 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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # Sprint 2 - 1 Key Errors (Answers) | ||
|
|
||
| ## 0.js | ||
| - Predicted error: | ||
| - Actual error: | ||
| - Why it happens: | ||
| - Minimal fix: | ||
| - Docs: | ||
|
|
||
| ## 1.js | ||
| - Predicted error: | ||
| - Actual error: | ||
| - Why it happens: | ||
| - Minimal fix: | ||
| - Docs: | ||
|
|
||
| ## 2.js | ||
| - Predicted error: | ||
| - Actual error: | ||
| - Why it happens: | ||
| - Minimal fix: | ||
|
|
||
|
|
||
|
|
||
| ## 0.js | ||
|
|
||
| - Predicted error: SyntaxError (variable redeclaration) | ||
|
|
||
| - Actual error: SyntaxError: Identifier 'str' has already been declared | ||
|
|
||
| - Why it happens: | ||
| The function parameter `str` is already declared. Inside the function, `let str = ...` attempts to redeclare the same variable in the same scope. JavaScript does not allow redeclaration of variables using `let`. | ||
|
|
||
| - Minimal fix: | ||
| Remove the `let` and return the new string directly: | ||
|
|
||
| ```js | ||
| function capitalise(str) { | ||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
|
|
||
| - Docs: | ||
| https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let | ||
|
|
||
|
|
||
|
|
||
| ## 1.js | ||
| - Predicted error: SyntaxError (variable redeclaration) | ||
|
|
||
| - Actual error: SyntaxError: Identifier 'decimalNumber' has already been declared | ||
|
|
||
| - Why it happens: | ||
| The function parameter `decimalNumber` is already declared. Inside the function, `const decimalNumber = 0.5;` attempts to redeclare the same variable in the same scope. JavaScript does not allow redeclaration using `const`. | ||
|
|
||
| There is also a second issue: | ||
| `console.log(decimalNumber);` is outside the function, and `decimalNumber` is not defined in the global scope. This would cause a ReferenceError. | ||
|
|
||
| - Concepts tested: | ||
| Variable scope, function parameters, redeclaration rules, global vs local variables. | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| return percentage; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.5)); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| ## 2.js | ||
|
|
||
| - Predicted error: SyntaxError (invalid function parameter) | ||
|
|
||
| - Actual error: SyntaxError: Unexpected number | ||
|
|
||
| - Why it happens: | ||
| The function is declared as `function square(3)`. | ||
| Function parameters must be variable names (identifiers), not literal values. | ||
| The number `3` is not a valid parameter name, so JavaScript throws a syntax error. | ||
|
|
||
| - Concept tested: | ||
| Function declaration syntax and valid parameter identifiers. | ||
|
|
||
| - Minimal fix: | ||
|
|
||
| ```js | ||
| function square(number) { | ||
| return number * number; | ||
| } | ||
|
|
||
| console.log(square(3)); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,29 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // The function will print 320 from inside multiply, | ||
| // but the final console.log will show "undefined" | ||
| // because multiply does not return a value. | ||
|
|
||
| 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 function multiply only logs the result using console.log, | ||
| // but it does not return the value. | ||
| // Since it does not return anything, JavaScript returns undefined by default. | ||
| // Therefore, the template string inserts 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,27 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // The function will return undefined because the return statement | ||
| // is followed by a line break. JavaScript inserts a semicolon automatically, | ||
| // so the function exits before a + b is executed. | ||
|
|
||
| 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 return statement is on its own line. | ||
| // JavaScript automatically inserts a semicolon after return. | ||
| // This means the function returns undefined immediately, | ||
| // and the expression a + b is never executed. | ||
|
|
||
| // 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 |
|---|---|---|
|
|
@@ -15,5 +15,6 @@ | |
| // It should return their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| const bmi = weight / (height * height); | ||
| return Number(bmi.toFixed(1)); | ||
| } | ||
|
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. Indentation is off. Can you fix it? Consider install prettier VSCode extension and enable formatting on save/paste on VSCode
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 @cjyuan, Thanks for the feedback. I have installed the Prettier VS Code extension and enabled format on save as recommended in the repository guidelines. I reformatted the files and pushed the updated changes to the branch. |
||
Uh oh!
There was an error while loading. Please reload this page.