|
2 | 2 |
|
3 | 3 | // Why will an error occur when this program runs? |
4 | 4 | // =============> write your prediction here |
5 | | - |
| 5 | +// If we run the program, I predict we will get an error. |
| 6 | +// The first problem is that 'decimalNumber' is already function parameter, but you try to declare another variable with the same name inside the function. |
| 7 | +//another problem is that 'decimalNumber' is a local variable inside the function |
| 8 | +// and we cannot access to it outside the function. |
| 9 | +//------------------------------------------------------ |
6 | 10 | // Try playing computer with the example to work out what is going on |
| 11 | +//SyntaxError: Identifier 'decimalNumber' has already been declared |
| 12 | + |
| 13 | +// =============> write your explanation here |
| 14 | + |
| 15 | +// The error occurs because 'decimalNumber' has already been declared |
| 16 | +// as a function parameter. we cannot declare it again with 'const' |
| 17 | + |
| 18 | +// Finally, correct the code to fix the problem |
| 19 | +// =============> write your new code here |
| 20 | +const decimalNumber = 0.5; |
7 | 21 |
|
8 | 22 | function convertToPercentage(decimalNumber) { |
9 | | - const decimalNumber = 0.5; |
10 | 23 | const percentage = `${decimalNumber * 100}%`; |
11 | 24 |
|
12 | 25 | return percentage; |
13 | 26 | } |
14 | 27 |
|
15 | | -console.log(decimalNumber); |
16 | | - |
17 | | -// =============> write your explanation here |
18 | | - |
19 | | -// Finally, correct the code to fix the problem |
20 | | -// =============> write your new code here |
| 28 | +console.log(convertToPercentage(decimalNumber)); |
0 commit comments