Skip to content

Commit 8a3c7d2

Browse files
committed
Fixed convertToPercentage function by removing variable shadowing and verified correct output
1 parent f2a2aba commit 8a3c7d2

File tree

1 file changed

+15
-0
lines changed
  • Sprint-2/1-key-errors

1 file changed

+15
-0
lines changed

Sprint-2/1-key-errors/1.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// Prediction: The error will occur because the variable 'decimalNumber' is being redeclared
6+
// inside the function using 'const', which is an illegal operation.
7+
// Variables declared with 'const' cannot be redeclared in the same scope.
58

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

11+
/*
12+
Original code:
813
function convertToPercentage(decimalNumber) {
914
const decimalNumber = 0.5;
1015
const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +18,18 @@ function convertToPercentage(decimalNumber) {
1318
}
1419
1520
console.log(decimalNumber);
21+
*/
1622

1723
// =============> write your explanation here
24+
// Explanation: The error occurs because 'decimalNumber' is declared as a parameter of the function.
25+
// When we try to declare it again inside the function with 'const', it causes a syntax error.
26+
// To fix this, we should either use the parameter directly or rename the inner variable.
1827

1928
// Finally, correct the code to fix the problem
2029
// =============> write your new code here
30+
function convertToPercentage(decimalNumber) {
31+
const percentage = `${decimalNumber * 100}%`;
32+
return percentage;
33+
}
34+
35+
console.log(convertToPercentage(0.5)); // Output: 50%

0 commit comments

Comments
 (0)