Skip to content

Commit 775070d

Browse files
committed
Fix percentage conversion function
1 parent 328f55f commit 775070d

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

  • Sprint-3/1-key-errors

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// Prediction: An error will occur because decimalNumber is already
5+
// declared as a function parameter and is declared again with const.
6+
// Also, decimalNumber is being used outside the function where it
7+
// is not available.
58

69
// Try playing computer with the example to work out what is going on
710

811
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
1012
const percentage = `${decimalNumber * 100}%`;
1113

1214
return percentage;
1315
}
1416

15-
console.log(decimalNumber);
17+
console.log(convertToPercentage(0.5));
1618

17-
// =============> write your explanation here
19+
// Explanation: The function parameter decimalNumber already contains
20+
// the value we give to the function. We cannot declare another
21+
// decimalNumber inside the function with const. The original code
22+
// also tried to use decimalNumber outside the function, where it
23+
// does not exist.
1824

1925
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
26+
27+
// New code: The function uses the decimalNumber parameter and returns
28+
// the percentage. We call the function with 0.5 and log the result.

0 commit comments

Comments
 (0)