22
33// Why will an error occur when this program runs?
44// =============> write your prediction here
5+ // An error will occur because 'decimalNumber' is redeclared inside the function using 'const',
6+ // which is not allowed. Also, 'decimalNumber' is not defined in the global scope,
7+ // so console.log(decimalNumber) will throw a ReferenceError.
58
69// Try playing computer with the example to work out what is going on
710
8- function convertToPercentage ( decimalNumber ) {
9- const decimalNumber = 0.5 ;
10- const percentage = `${ decimalNumber * 100 } %` ;
11+ // function convertToPercentage(decimalNumber) {
12+ // const decimalNumber = 0.5;
13+ // const percentage = `${decimalNumber * 100}%`;
1114
12- return percentage ;
13- }
15+ // return percentage;
16+ // }
1417
15- console . log ( decimalNumber ) ;
18+ // console.log(decimalNumber);
1619
1720// =============> write your explanation here
21+ // The error occurs because we cannot redeclare the parameter 'decimalNumber' inside the function
22+ // using 'const'. Also, 'decimalNumber' is not defined outside the function, so
23+ // console.log(decimalNumber) will throw a ReferenceError.
1824
1925// Finally, correct the code to fix the problem
2026// =============> write your new code here
27+
28+ function convertToPercentage ( decimalNumber ) {
29+ const percentage = `${ decimalNumber * 100 } %` ;
30+ return percentage ;
31+ }
32+
33+ console . log ( convertToPercentage ( 0.9 ) ) ; // Output: 90%
0 commit comments