11// Predict and explain first...
2+ // At the first glance, I believe the function would occur errors because the variable decimalNumber is declared twice.
3+ // Besides, console.log is trying to log a variable that is not defined in global scope.
24
35// Why will an error occur when this program runs?
46// =============> write your prediction here
7+ // The first error should be a syntax error that "decimalNumber" has already been declared.
8+ // Since decimalNumber is declared as a parameter of the function,
9+ // when a new variable also named decimalNumber is declared inside the function,
10+ // it causes a conflict.
511
6- // Try playing computer with the example to work out what is going on
12+ // The second error should be a reference error that "decimalNumber is not defined".
713
14+ // Try playing computer with the example to work out what is going on
15+ /*
816function convertToPercentage(decimalNumber) {
917 const decimalNumber = 0.5;
1018 const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +21,19 @@ function convertToPercentage(decimalNumber) {
1321}
1422
1523console.log(decimalNumber);
24+ */
1625
1726// =============> write your explanation here
27+ // After running the code, I got a syntax error that "decimalNumber" has already been declared.
28+ // I also found out that the decimalNumber inside the function should be declared again.
29+ // Besides, if we want to log the result of the function, we should call the function with an argument inside console.log().
1830
1931// Finally, correct the code to fix the problem
2032// =============> write your new code here
33+ function convertToPercentage ( decimalNumber ) {
34+ const percentage = `${ decimalNumber * 100 } %` ;
35+
36+ return percentage ;
37+ }
38+ console . log ( convertToPercentage ( 0.5 ) ) ; // should return "50%"
39+ console . log ( convertToPercentage ( 0.75 ) ) ; // should return "75%"
0 commit comments