Skip to content

Commit 01b0572

Browse files
fix key error in 1.js - duplicate decimalNumber variable and scope issue
1 parent 69c00ce commit 01b0572

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// Predict and explain first...
22

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// Predict and explain first...
5+
// I predict this will cause a SyntaxError because decimalNumber is declared twice in the same scope (once as a parameter, once with const). Even after fixing that, the console.log on line 15 would still fail because decimalNumber only exists inside the function.
56

7+
// Why will an error occur when this program runs?
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);
16-
17-
// =============> write your explanation here
15+
console.log(convertToPercentage(0.5));
1816

19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
17+
// decimalNumber is declared twice in the same scope, once as the function's parameter, and again with const inside the function body. JavaScript doesn't allow redeclaring the same name in the same scope. Even after fixing that, console.log(decimalNumber) would fail too, because decimalNumber only exists inside the function.

0 commit comments

Comments
 (0)