File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed
Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change 22
33// Why will an error occur when this program runs?
44// =============> write your prediction here
5+ // Prediction: The error will occur because the variable 'decimalNumber' is being redeclared
6+ // inside the function using 'const', which is an illegal operation.
7+ // Variables declared with 'const' cannot be redeclared in the same scope.
58
69// Try playing computer with the example to work out what is going on
710
11+ /*
12+ Original code:
813function convertToPercentage(decimalNumber) {
914 const decimalNumber = 0.5;
1015 const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +18,18 @@ function convertToPercentage(decimalNumber) {
1318}
1419
1520console.log(decimalNumber);
21+ */
1622
1723// =============> write your explanation here
24+ // Explanation: The error occurs because 'decimalNumber' is declared as a parameter of the function.
25+ // When we try to declare it again inside the function with 'const', it causes a syntax error.
26+ // To fix this, we should either use the parameter directly or rename the inner variable.
1827
1928// Finally, correct the code to fix the problem
2029// =============> write your new code here
30+ function convertToPercentage ( decimalNumber ) {
31+ const percentage = `${ decimalNumber * 100 } %` ;
32+ return percentage ;
33+ }
34+
35+ console . log ( convertToPercentage ( 0.5 ) ) ; // Output: 50%
You can’t perform that action at this time.
0 commit comments