File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
2+ // =============> write your prediction here
3+ // I predict this program will throw a `SyntaxError` because
4+ // the parameter `decimalNumber` is being redeclared inside the function using `const`.
5+ // Also, the variable `decimalNumber` is being accessed outside the function where it's not defined.
6+
27
38// Why will an error occur when this program runs?
49// =============> write your prediction here
@@ -15,6 +20,20 @@ function convertToPercentage(decimalNumber) {
1520console . log ( decimalNumber ) ;
1621
1722// =============> write your explanation here
23+ // The function already has a parameter named `decimalNumber`,
24+ // but inside the function, it's trying to declare another
25+ // constant with the same name: `const decimalNumber = 0.5;`.
26+ // JavaScript doesn’t allow redeclaring a variable that already exists in the same scope.
27+ // Also, `console.log(decimalNumber)` is outside the function and `decimalNumber` is not
28+ // defined in the global scope, so that will cause a `ReferenceError`.
29+
1830
1931// Finally, correct the code to fix the problem
2032// =============> write your new code here
33+
34+ function convertToPercentage ( decimalNumber ) {
35+ const percentage = `${ decimalNumber * 100 } %` ;
36+ return percentage ;
37+ }
38+
39+ console . log ( convertToPercentage ( 0.5 ) ) ;
You can’t perform that action at this time.
0 commit comments