Skip to content

Commit e198d25

Browse files
committed
Add SyntaxError explanation
1 parent d973f84 commit e198d25

File tree

1 file changed

+18
-2
lines changed
  • Sprint-2/1-key-errors

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// I predict that this program will throw a error - due to the variable decimalNumber being redeclared.
56

67
// Try playing computer with the example to work out what is going on
78

8-
function convertToPercentage(decimalNumber) {
9+
/* function convertToPercentage(decimalNumber) {
910
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
1112
1213
return percentage;
1314
}
1415
15-
console.log(decimalNumber);
16+
console.log(decimalNumber); */
1617

1718
// =============> write your explanation here
19+
// Function covertToPercentage(decimalNumber) - it has a parameter named decimalNumber
20+
// Inside the function: const decimalNumber =0.5; - this redeclares the same variable name that's already used for the parameter.
21+
// That causes an error: SyntaxError: Identifier 'decimalNumber' has already been declared - because you can't declare a const (or let) with the same name as a parameter inside the same function scope.
22+
// JavaScript doesn't allow us to declare a new variable with the same name in the same scope, so it caused an error.
1823

1924
// Finally, correct the code to fix the problem
2025
// =============> write your new code here
26+
function convertToPercentage(decimalNumber) {
27+
const percentage = `${decimalNumber * 100}%`;
28+
return percentage;
29+
}
30+
31+
console.log(convertToPercentage(0.5));
32+
33+
// Function decimalNumber = 0.5
34+
// It calculates 0.5 * 100 = 50
35+
// it returns "50%"
36+
// console logs 50%

0 commit comments

Comments
 (0)