Skip to content

Commit 7dfe904

Browse files
committed
explained the errors and output of 1,js file
1 parent b61cf21 commit 7dfe904

File tree

1 file changed

+16
-6
lines changed
  • Sprint-2/1-key-errors

1 file changed

+16
-6
lines changed

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

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

33
// Why will an error occur when this program runs?
44
// =============> write your prediction here
5+
//The program will return undefined because the console log which is outside the function is calling a local variable within the function scope
56

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
19+
// The program did not return undefined on first call, but rather returned an identifier redeclaration syntaxerror. However when corrected without variable redeclaration, it returns undefined because console.log(decimalNumber) is not defined.
1820

1921
// Finally, correct the code to fix the problem
2022
// =============> write your new code here
23+
function convertToPercentage(decimalNumber) {
24+
decimalNumber = 0.5;
25+
const percentage = `${decimalNumber * 100}%`;
26+
27+
return percentage;
28+
}
29+
30+
console.log(convertToPercentage());

0 commit comments

Comments
 (0)