-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path1.js
More file actions
33 lines (23 loc) · 1.46 KB
/
1.js
File metadata and controls
33 lines (23 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Predict and explain first...
// Why will an error occur when this program runs?
// The error occurs because the variable decimalNumber is declared twice in the function convertToPercentage. The first declaration is in the function parameter, and the second declaration is inside the function body. This causes a conflict because you cannot declare a variable with the same name in the same scope.
// =============> write your prediction here
// The command is to convert a decimal number to a percentage.
// Try playing computer with the example to work out what is going on
//
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
console.log(decimalNumber);
// =============> write your explanation here
// The function convertToPercentage takes a decimal number as input and attempts to convert it to a percentage. However, the variable decimalNumber is declared twice, which causes a SyntaxError. To fix this, we can remove the second declaration of decimalNumber inside the function body and directly return the calculated percentage.
// Finally, correct the code to fix the problem
// =============> write your new code here
// The corrected function should look like this:
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}
// Now the function should work correctly and convert a decimal number to a percentage.