Skip to content

Commit 04cdbd3

Browse files
committed
Fix variable redeclaration issues and enhance error prediction comments in key error examples
1 parent 0823433 commit 04cdbd3

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

Sprint-2/1-key-errors/0.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
32

4-
// call the function capitalise with a string input
3+
// =============> write your prediction here
4+
//A) You are likely to encounter a syntax error because the variable `str` is being declared twice within the same scope
55
// interpret the error message and figure out why an error is occurring
66

77
function capitalise(str) {
8-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
9-
return str;
8+
let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`;
9+
return capitalisedStr;
1010
}
1111

12-
// =============> write your explanation here
13-
// =============> write your new code here
12+
console.log(capitalise("hello"));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// An error will occur because the variable `decimalNumber` is being declared twice within the same scope
6+
57

68
// Try playing computer with the example to work out what is going on
79

810
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
11-
1212
return percentage;
1313
}
1414

15-
console.log(decimalNumber);
15+
console.log(convertToPercentage(0.5));
1616

1717
// =============> write your explanation here
1818

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44
// this function should square any number but instead we're going to get an error
55

66
// =============> write your prediction of the error here
7+
// an error will occur because the value `3` has been put in the parentheses of the function.
78

8-
function square(3) {
9+
10+
function square(num) {
911
return num * num;
1012
}
13+
console.log(square(3));
1114

1215
// =============> write the error message here
16+
// SyntaxError: Unexpected number
1317

1418
// =============> explain this error message here
19+
// An error occurs because the function parameter `3` is a literal value instead of a variable name.
1520

1621
// Finally, correct the code to fix the problem
1722

23+
1824
// =============> write your new code here
1925

2026

0 commit comments

Comments
 (0)