22
33// Why will an error occur when this program runs?
44// =============> write your prediction here
5+ // I thought the function will work properly but couldn't print the correct value cause the value should be convertToPercentage
6+ // but it is declared only as decimalNumber.
57
68// Try playing computer with the example to work out what is going on
79
8- function convertToPercentage ( decimalNumber ) {
9- const decimalNumber = 0.5 ;
10- const percentage = `${ decimalNumber * 100 } %` ;
10+ // function convertToPercentage(decimalNumber) {
11+ // const decimalNumber = 0.5;
12+ // const percentage = `${decimalNumber * 100}%`;
1113
12- return percentage ;
13- }
14+ // return percentage;
15+ // }
1416
15- console . log ( decimalNumber ) ;
17+ // console.log(decimalNumber);
1618
1719// =============> write your explanation here
20+ // When I run the code after my prediction it shows SyntaxError: Identifier 'decimalNumber' has already been declared. Which means
21+ // decimalNumber has been declared already as function parameter and then again declared inside the function as variable. Then when I
22+ // declare const decimalNumber = 0.5; outside the function and run the code it shows the value of variable decimalNumber which is 0.5. Then
23+ // I replace console.log(decimalNumber); to console.log(convertToPercentage); and run the code again and it shows this output
24+ // [Function: convertToPercentage] which means only the funtion name. Then I replace console.log(convertToPercentage); to
25+ // console.log(convertToPercentage(decimalNumber)); again and it finally works properly that means converted the decimal number to
26+ // percentage value which shows 50% .
27+
1828
1929// Finally, correct the code to fix the problem
2030// =============> write your new code here
31+ // Corrected code:
32+
33+ function convertToPercentage ( decimalNumber ) {
34+ const percentage = `${ decimalNumber * 100 } %` ;
35+ return percentage ;
36+ }
37+ const decimalNumber = 0.5 ;
38+ console . log ( convertToPercentage ( decimalNumber ) ) ;
0 commit comments