|
2 | 2 |
|
3 | 3 | // Why will an error occur when this program runs? |
4 | 4 | // =============> write your prediction here |
| 5 | +// I predict that the decimalNumber variable will be timed 100 and will get a percentage value of 50%, but that only my predictions. |
5 | 6 |
|
6 | 7 | // Try playing computer with the example to work out what is going on |
7 | 8 |
|
8 | | -function convertToPercentage(decimalNumber) { |
9 | | - const decimalNumber = 0.5; |
10 | | - const percentage = `${decimalNumber * 100}%`; |
| 9 | +// function convertToPercentage(decimalNumber) { |
| 10 | +// // const decimalNumber = 0.5; |
| 11 | +// const percentage = `${decimalNumber * 100}%`; |
11 | 12 |
|
12 | | - return percentage; |
13 | | -} |
| 13 | +// return percentage; |
| 14 | +// } |
14 | 15 |
|
15 | | -console.log(decimalNumber); |
| 16 | +// console.log(decimalNumber); |
16 | 17 |
|
17 | 18 | // =============> write your explanation here |
| 19 | +// SyntaxError: Identifier 'decimalNumber' has already been declared |
| 20 | +// This error occurs because we have declared the variable decimalNumber twice, |
| 21 | +// once as a parameter and once as a variable inside the function. |
| 22 | + |
| 23 | +// apparently there is another error that we only console logged the variable decimalNumber which is not declared in the global scope, |
| 24 | +// to fix this we instead call the function convertToPercentage with a decimal number as an argument and log the result of that function call to the console. |
18 | 25 |
|
19 | 26 | // Finally, correct the code to fix the problem |
20 | 27 | // =============> write your new code here |
| 28 | + |
| 29 | +function convertToPercentage(decimalNumber) { |
| 30 | + const percentage = `${decimalNumber * 100}%`; |
| 31 | + |
| 32 | + return percentage; |
| 33 | +} |
| 34 | + |
| 35 | +console.log(convertToPercentage(0.5)); |
0 commit comments