File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // Prediction: Node will report a SyntaxError because str is declared twice in
3+ // the same function scope: once as a parameter and again with let.
34
45// call the function capitalise with a string input
56// interpret the error message and figure out why an error is occurring
67
8+ // Faulty line from the original: let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9+
10+ // Error: SyntaxError: Identifier 'str' has already been declared
11+ // Explanation: A function parameter is already a binding in the function body.
12+ // Declaring another let binding with the same name is not allowed.
13+ // Corrected code:
714function capitalise ( str ) {
8- let str = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
9- return str ;
15+ return `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
1016}
1117
12- // =============> write your explanation here
13- // =============> write your new code here
18+ console . log ( capitalise ( "hello" ) ) ;
Original file line number Diff line number Diff line change 11// Predict and explain first...
22
33// Why will an error occur when this program runs?
4- // =============> write your prediction here
4+ // Prediction: Node will report a SyntaxError because decimalNumber is declared
5+ // as a const inside the function even though it is already a parameter.
56
67// Try playing computer with the example to work out what is going on
78
8- function convertToPercentage ( decimalNumber ) {
9- const decimalNumber = 0.5 ;
10- const percentage = `${ decimalNumber * 100 } %` ;
11-
12- return percentage ;
13- }
14-
15- console . log ( decimalNumber ) ;
9+ // Faulty original lines:
10+ // const decimalNumber = 0.5; // duplicates the parameter inside the function
11+ // console.log(decimalNumber); // refers to the parameter outside its scope
1612
17- // =============> write your explanation here
13+ // Error: SyntaxError: Identifier 'decimalNumber' has already been declared
14+ // Explanation: The parameter already declares decimalNumber in the function
15+ // scope. The inner const declaration duplicates that binding. Also, the final
16+ // console.log refers to a name that would not be available outside the function.
1817
1918// Finally, correct the code to fix the problem
20- // =============> write your new code here
19+ function convertToPercentage ( decimalNumber ) {
20+ return `${ decimalNumber * 100 } %` ;
21+ }
22+
23+ console . log ( convertToPercentage ( 0.5 ) ) ;
Original file line number Diff line number Diff line change 33
44// this function should square any number but instead we're going to get an error
55
6- // =============> write your prediction of the error here
6+ // Prediction: Node will report a SyntaxError because a number cannot be used
7+ // as a function parameter name.
78
8- function square ( 3 ) {
9- return num * num ;
10- }
9+ // Faulty original: function square(3) { return num * num; }
1110
12- // =============> write the error message here
11+ // Error: SyntaxError: Unexpected number
1312
14- // =============> explain this error message here
13+ // Explanation: Function parameters must be identifiers such as num, not literal
14+ // values such as 3. The body also refers to num, which was never declared.
1515
1616// Finally, correct the code to fix the problem
1717
18- // =============> write your new code here
18+ function square ( num ) {
19+ return num * num ;
20+ }
21+
22+ console . log ( square ( 3 ) ) ;
1923
2024
You can’t perform that action at this time.
0 commit comments