11// Predict and explain first...
2+ // the code is supposed to convert a decimal number to percentage by multiplying
3+ // the decimal Number by 100 then add the sign % to show that it is a percentage
24
35// Why will an error occur when this program runs?
46// =============> write your prediction here
7+ // an error will occur because the variable decimalNumber is redeclared inside the function.
8+ // it's already declared as a parameter of the function, so redeclaring it with const causes a syntax error.
59
610// Try playing computer with the example to work out what is going on
711
@@ -15,6 +19,18 @@ function convertToPercentage(decimalNumber) {
1519console . log ( decimalNumber ) ;
1620
1721// =============> write your explanation here
22+ // the fucntion convertToPercentage has a parameter named decimalNumber,
23+ // but inside the function, there is a line that tries to declare a new constant with the same name decimalNumber.
24+ // there is no need to redeclare the variable or edit it since it is a user input.
25+ // also the variable decimalNumber is called from outside the function which menas from another scope.
26+ // that is wrong because the variable can only be used inside the fucntion. instead of that we need to call the funtion, not hte variable.
1827
1928// Finally, correct the code to fix the problem
2029// =============> write your new code here
30+
31+ function convertToPercentage ( decimalNumber ) {
32+ const percentage = `${ decimalNumber * 100 } %` ;
33+ return percentage ;
34+ }
35+
36+ console . log ( convertToPercentage ( 0.3 ) ) ;
0 commit comments