2
2
3
3
// Why will an error occur when this program runs?
4
4
// =============> 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.
5
8
6
9
// Try playing computer with the example to work out what is going on
7
10
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}%`;
11
14
12
- return percentage ;
13
- }
15
+ // return percentage;
16
+ // }
14
17
15
- console . log ( decimalNumber ) ;
18
+ // console.log(decimalNumber);
16
19
17
20
// =============> 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.
18
24
19
25
// Finally, correct the code to fix the problem
20
26
// =============> 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