|
2 | 2 |
|
3 | 3 | // Why will an error occur when this program runs? |
4 | 4 | // =============> write your prediction here |
| 5 | +// similar problem, the identifier has already been declared in function. |
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 | +// similar problem, the identifier 'decimalNumber" has already been declared in function and is a parameter. |
| 20 | +// we can then remove const decimalNumber, then function can also receive different numbers. |
| 21 | +// const percentage = `${decimalNumber * 100} %`;----> this calculates decimalNumber in which case it is 0.5 and multiply it by 100 which = 50 and then we add the % at the end which equals to 50% |
| 22 | +// another error is the console.log that exists outside the function and is tryng to log something that exists inside the function. |
| 23 | +// we want to print the result of the function we can ---> console.log(convertToPercentage(0.5)); |
18 | 24 |
|
19 | 25 | // Finally, correct the code to fix the problem |
20 | 26 | // =============> write your new code here |
| 27 | + |
| 28 | +function convertToPercentage(decimalNumber) { |
| 29 | + const percentage = `${decimalNumber * 100}%`; |
| 30 | + |
| 31 | + return percentage; |
| 32 | +} |
| 33 | + |
| 34 | +console.log(convertToPercentage(0.5)); |
0 commit comments