You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/1.js
+15-2Lines changed: 15 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -3,18 +3,31 @@
3
3
// Why will an error occur when this program runs?
4
4
// =============> write your prediction here
5
5
6
+
//Prediction: The program will fail with a SyntaxError because 'decimalNumber' is redeclared.
7
+
6
8
// Try playing computer with the example to work out what is going on
7
9
8
10
functionconvertToPercentage(decimalNumber){
9
-
constdecimalNumber=0.5;
11
+
//const decimalNumber = 0.5; <--- this line causes the syntaxError.
10
12
constpercentage=`${decimalNumber*100}%`;
11
13
12
14
returnpercentage;
13
15
}
14
16
15
-
console.log(decimalNumber);
17
+
//console.log(decimalNumber); <--- this line causes the ReferenceError.
16
18
17
19
// =============> write your explanation here
20
+
//1. Inside the function: Just like the previous example, the parameter `decimalNumber` acts as a local variable. Trying to redeclare it with `const decimalNumber = 0.5;` causes a crash.
21
+
// Additionally, hardcoding 0.5 defeats the purpose of having a parameter at all.
22
+
23
+
// 2. Outside the function: The variable `decimalNumber` is "scoped" to the function. It doesn't exist in the outside world. When `console.log(decimalNumber)` runs globally, JavaScript doesn't know what it is,
24
+
// causing a ReferenceError. You need to call the function and pass the number as an argument instead.
0 commit comments