Skip to content

Commit ba92053

Browse files
committed
Complete Sprint 3 section 1: errors
1 parent d28242a commit ba92053

3 files changed

Lines changed: 35 additions & 23 deletions

File tree

‎Sprint-3/1-key-errors/0.js‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
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:
714
function 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"));

‎Sprint-3/1-key-errors/1.js‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
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));

‎Sprint-3/1-key-errors/2.js‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
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

0 commit comments

Comments
 (0)