Skip to content

Commit a8976b0

Browse files
committed
Fix redeclaration error in convertToPercentage function and update comments for clarity
1 parent 13fc59b commit a8976b0

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5-
5+
//An error will occur because the variable "decimalNumber"
6+
//is being declared twice in the same scope.
7+
//The parameter "decimalNumber" is already defined as a
8+
//function argument, and then it is being redeclared
9+
//with "const decimalNumber = 0.5;". This causes a
10+
//syntax error because we cannot declare a variable
11+
//with the same name in the same scope.
612
// Try playing computer with the example to work out what is going on
713

814
function convertToPercentage(decimalNumber) {
@@ -16,5 +22,20 @@ console.log(decimalNumber);
1622

1723
// =============> write your explanation here
1824

25+
//The error occurs because the variable "decimalNumber" is being
26+
//declared twice in the same scope. The parameter "decimalNumber"
27+
//is already defined as a function argument, and then it is being
28+
//redeclared with "const decimalNumber = 0.5;". This causes a
29+
//syntax error because we cannot declare a variable with the
30+
//same name in the same scope. To fix this, we can either rename
31+
//the parameter or the variable inside the function.
32+
1933
// Finally, correct the code to fix the problem
2034
// =============> write your new code here
35+
function convertToPercentage(decimalNumber) {
36+
const percentage = `${decimalNumber * 100}%`;
37+
38+
return percentage;
39+
}
40+
41+
console.log(convertToPercentage(0.5));

0 commit comments

Comments
 (0)