Skip to content

Commit 4eaa3d3

Browse files
committed
Fix variable redeclaration and update function call in percentage conversion
1 parent 2a3a27d commit 4eaa3d3

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 17 additions & 5 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+
// I predict the code will generate 2 errors because one the variable decimalNumber is declared twice and two
5+
// the function is not called instead the variable decimalNumber is called and it can't be accessed because
6+
// it was declared locally as a parameter of the function
57

68
// Try playing computer with the example to work out what is going on
79

8-
function convertToPercentage(decimalNumber) {
10+
/*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(decimalNumber);*/
1618

17-
// =============> write your explanation here
19+
// After we run the code, it generated an error SyntaxError: Identifier 'decimalNumber' has already been declared
20+
// because we tried to declare decimalNumber again. After fixing this error again we got another error ReferenceError: decimalNumber is not defined
21+
// because the decimalNumber is locally scoped, javascript can't access it when the function is called
22+
// additionally the function was not called console.log(decimalNumber) this simply means print the value of decimalNumber
1823

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

0 commit comments

Comments
 (0)