Skip to content

Commit a44bb25

Browse files
committed
Explaining the error, prediction, and fix
1 parent 15e3b83 commit a44bb25

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

  • Sprint-3/1-key-errors

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

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

33
// Why will an error occur when this program runs?
4+
//- variable name "decimalNumber" declared twice in the same function. one as function parameter and again as a const in the same function.
45
// =============> write your prediction here
5-
6+
//- SyntaxError because "decimalNumber" declared twice.
67
// Try playing computer with the example to work out what is going on
78

8-
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
9+
// function convertToPercentage(decimalNumber) {
10+
// const decimalNumber= 0.5;
11+
// const percentage = `${decimalNumber * 100}%`;
1112

12-
return percentage;
13-
}
13+
// return percentage;
14+
// }
1415

15-
console.log(decimalNumber);
16+
// console.log(decimalNumber);
1617

1718
// =============> write your explanation here
18-
19+
//- "decimalNumber" is already provided as a parameter to the function, but I don't need a parameter because I'm creating the value "0.5" inside the function. So I removed the parameter. I also need to call the function with convertToPercentage() instead of console.log(decimalNumber) because "decimalNumber" only exists inside the function.
1920
// Finally, correct the code to fix the problem
2021
// =============> write your new code here
22+
23+
function convertToPercentage() {
24+
const decimalNumber = 0.5;
25+
const percentage = `${decimalNumber * 100}%`;
26+
27+
return percentage;
28+
}
29+
30+
console.log(convertToPercentage());

0 commit comments

Comments
 (0)