Skip to content

Commit d67f3f3

Browse files
committed
key error 0.1
1 parent 1b8e8a1 commit d67f3f3

File tree

1 file changed

+18
-8
lines changed
  • Sprint-2/1-key-errors

1 file changed

+18
-8
lines changed

Sprint-2/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-
// =============> write your prediction here
4+
// Here the name decimalNumber in 'const decimalNumber' has already been used, so we will get a synthax Error for that line.
55

66
// Try playing computer with the example to work out what is going on
77

8+
// function convertToPercentage(decimalNumber) {
9+
// const decimalNumber = 0.5;
10+
// const percentage = `${decimalNumber * 100}%`;
11+
12+
// return percentage;
13+
// }
14+
15+
// console.log(decimalNumber);
16+
17+
// JavaScript is read from top to bottom, so when the processor reads an error, the program will stop running and will get the error message.
18+
//The first error is on line 9 with the reuse of the variable declaration name of 'decimalNumber.
19+
//The second error is on line 15 where we are trying to call the variable 'decimalNumber',which is a local variable inside the function. We cannot access this variable outside of the function. We will need to call the whole function.
20+
// Finally, correct the code to fix the problem
21+
// =============> write your new code here
822
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
10-
const percentage = `${decimalNumber * 100}%`;
23+
const decimalNumber1 = 0.5;
24+
const percentage = `${decimalNumber1 * 100}%`;
1125

1226
return percentage;
1327
}
1428

15-
console.log(decimalNumber);
16-
17-
// =============> write your explanation here
29+
console.log(convertToPercentage());
1830

19-
// Finally, correct the code to fix the problem
20-
// =============> write your new code here

0 commit comments

Comments
 (0)