Skip to content

Commit dc3752d

Browse files
Fix % conversion
1 parent d83f34c commit dc3752d

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

  • Sprint-3/1-key-errors

‎Sprint-3/1-key-errors/1.js‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
// Predict and explain first...
22

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// Because decimalNumber is declared twice, once as a parameter and again with const
55

66
// Try playing computer with the example to work out what is going on
77

8+
/*
9+
810
function convertToPercentage(decimalNumber) {
911
const decimalNumber = 0.5;
1012
const percentage = `${decimalNumber * 100}%`;
1113
1214
return percentage;
1315
}
1416
15-
console.log(decimalNumber);
17+
console.log(convertToPercentage(decimalNumber)); */
1618

17-
// =============> write your explanation here
19+
// decimalNumber is already a parameter, so declaring it again with const causes an error
20+
// decimalNumber has already been declared as a parameter in the same function scope
21+
// console.log(decimalNumber) is wrong because decimalNumber is not defined outside the function,
22+
// so we need to call the function with a value
1823

1924
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
25+
26+
function convertToPercentage(decimalNumber) {
27+
const percentage = `${decimalNumber * 100}%`;
28+
29+
return percentage;
30+
}
31+
32+
console.log(convertToPercentage("26"));

0 commit comments

Comments
 (0)