Skip to content

Commit b2459ed

Browse files
Added actual error output to comments and restored working code
1 parent 297d063 commit b2459ed

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

  • Sprint-3/1-key-errors

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

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

33
// Why will an error occur when this program runs?
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.
4+
// I predict this will cause a SyntaxError first (at parse time) because
5+
// decimalNumber is declared twice in the same scope.
66

7-
// Why will an error occur when this program runs?
8-
// Try playing computer with the example to work out what is going on
7+
// Once the SyntaxError is fixed, a ReferenceError will appear at runtime.
8+
// This is because we would need to rename one of the variables so the
9+
// function can actually use it.
910

1011
function convertToPercentage(decimalNumber) {
11-
const percentage = `${decimalNumber * 100}%`;
12+
const percentage = `${decimalNumber * 100}%`;
1213
return percentage;
1314
}
1415

1516
console.log(convertToPercentage(0.5));
1617

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.
18+
// 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.
19+
//ACTUAL ERROR (SyntaxError):.
20+
// C:\Users\EarthHarvestAdmin\CYF2026 LAMLA KAMANA\Module-JavaScript-Fundamentals\Sprint-3\1-key-errors\1.js:12
21+
// const decimalNumber = `$ {decimalNumber * 100}%`;
22+
// ^
23+
24+
// SyntaxError: Identifier 'decimalNumber' has already been declared

0 commit comments

Comments
 (0)