Skip to content

Commit fa98548

Browse files
committed
fix proble, answer questions
1 parent 71235b3 commit fa98548

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

  • Sprint-2/1-key-errors

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
// Predict and explain first...
2-
32
// Why will an error occur when this program runs?
43
// =============> write your prediction here
5-
4+
// I think there will be an error as "decimalNumber" variable has already been declared as a parameter in
5+
// "convertToPercentage" function
6+
//
67
// Try playing computer with the example to work out what is going on
78

8-
function convertToPercentage(decimalNumber) {
9+
/*function convertToPercentage(decimalNumber) {
910
const decimalNumber = 0.5;
1011
const percentage = `${decimalNumber * 100}%`;
1112
1213
return percentage;
1314
}
1415
15-
console.log(decimalNumber);
16+
console.log(decimalNumber); */
1617

1718
// =============> write your explanation here
19+
// =============Explanation ===============
20+
// the "decimalNumber" variable is a local variable and can only be seen inside "convertToPercentage" function
21+
// therefore when called outside the function it triggers "ReferenceError"
1822

1923
// Finally, correct the code to fix the problem
24+
// =========correction=========
25+
// problem can be fixed by moving the variable declaration outside the function
26+
// and making it global
27+
2028
// =============> write your new code here
29+
30+
const decimalNumber = 0.5;
31+
function convertToPercentage(decimalNumber) {
32+
const percentage = `${decimalNumber * 100}%`;
33+
34+
return percentage;
35+
}
36+
37+
console.log(decimalNumber);
38+
console.log(convertToPercentage(decimalNumber));
39+
40+

0 commit comments

Comments
 (0)