22
33// Why will an error occur when this program runs?
44// =============> write your prediction here
5+ // The percentage sign is not seen as a text character but instead as code for
6+ // the function to calculate the remainder after a division operation: the modulo.
7+ // It will therefore not return a percentage as desired
8+
59
610// Try playing computer with the example to work out what is going on
711
@@ -15,6 +19,24 @@ function convertToPercentage(decimalNumber) {
1519console . log ( decimalNumber ) ;
1620
1721// =============> write your explanation here
22+ // The first error is that the variable decimalNumber is being declared twice,
23+ // first as a parameter and then as a local variable inside the function. This
24+ // will cause a syntax error.
25+ // The second error is that the variable decimalNumber is not defined outside the
26+ // function. It will not log to the console but throw a ReferenceError because it
27+ // is not accessible in that scope.
28+
29+ // Also, on testing the code I saw that I had misjudged the modulo issue - it is not
30+ // an issue at all because the percentage sign is being used as a text character in a
31+ // template literal, so it will be treated as such and not as code for the modulo operator.
1832
1933// Finally, correct the code to fix the problem
2034// =============> write your new code here
35+ function convertToPercentage ( decimalNumber ) {
36+
37+ const percentage = `${ decimalNumber * 100 } %` ;
38+
39+ return percentage ;
40+ }
41+
42+ console . log ( convertToPercentage ( 0.5 ) ) ;
0 commit comments