11// Predict and explain first...
22
33// Why will an error occur when this program runs?
4- // =============> write your prediction here
4+ // =============> decimalNumber is defined within the function so it will not be recognised in console.log(decimalNumber);
55
66// Try playing computer with the example to work out what is going on
77
@@ -14,7 +14,24 @@ function convertToPercentage(decimalNumber) {
1414
1515console . log ( decimalNumber ) ;
1616
17- // =============> write your explanation here
17+ // =============> this is a function to convert a decimal number to a percentage - function declared
18+ // the decimal number is declared as 0.5
19+ // percentage is calculated by multiplying the decimal // number by 100 and then adding the % symbol
20+ // the answer returned is named percentage and gives a // number followed by %
21+ // the answer will be printed in console
1822
1923// Finally, correct the code to fix the problem
20- // =============> write your new code here
24+ // =============> // the error was actually 'decimalNumber' has already been declared
25+ // this was because decimalNumber was declared inside the function convertToPercentage(decimalNumber)
26+ // and declared again within the function with the const statement
27+ // once const decimalNumber = 0.5; was moved outside the function this error was resolved
28+ // that way the global decimalNumber exists and the function can still accept it as an argument
29+
30+
31+ // const decimalNumber = 0.5;
32+ // function convertToPercentage(decimalNumber) {
33+ // const percentage = `${decimalNumber * 100}%`;
34+ // return percentage;
35+ // }
36+ // console.log(decimalNumber);
37+
0 commit comments