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
{{ message }}
Repository navigation
Commit b2459ed
Browse filesBrowse the repository at this point in the historyBrowse files
// 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.
6
6
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.
9
10
10
11
functionconvertToPercentage(decimalNumber){
11
-
constpercentage=`${decimalNumber*100}%`;
12
+
constpercentage=`${decimalNumber*100}%`;
12
13
returnpercentage;
13
14
}
14
15
15
16
console.log(convertToPercentage(0.5));
16
17
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.
0 commit comments