Skip to content

Commit ac1886b

Browse files
committed
Predicted, explained and fixed the code errors.
1 parent 194c793 commit ac1886b

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

  • Sprint-3/1-key-errors

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@
22

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
// decimalNumber declared inside the function, this is the value that must be an arg
6+
// line 17 also fails because it tries to access a var that is declared within the function
57

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

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

12-
return percentage;
13-
}
14+
// return percentage;
15+
// }
1416

15-
console.log(decimalNumber);
17+
// console.log(decimalNumber);
1618

1719
// =============> write your explanation here
20+
// Remove the local var declaration
21+
// ReferenceError: decimalNumber is not defined | console.log(decimalNumber);
22+
// define decimalNumber in line 17 by passing the function with decimalNumber value
23+
// remove the definition in line 11
1824

1925
// Finally, correct the code to fix the problem
2026
// =============> write your new code here
27+
28+
function convertToPercentage(decimalNumber) {
29+
const percentage = `${decimalNumber * 100}%`;
30+
31+
return percentage;
32+
}
33+
34+
console.log(convertToPercentage(0.5));

0 commit comments

Comments
 (0)