Skip to content

Commit 6d602d8

Browse files
committed
fix duplicate declaration and print call in sprint 3 key error 1
1 parent e52f37a commit 6d602d8

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

  • Sprint-3/1-key-errors

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

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

3-
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
5-
3+
// Why will an error occur when this program runs
4+
// When this program runs there will be an identifier SyntaxError because 'decimalNumber' has already been declared
5+
// Also the output will print a decimal since it says console.log(decimalNumber);
66
// Try playing computer with the example to work out what is going on
77

88
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
109
const percentage = `${decimalNumber * 100}%`;
1110

1211
return percentage;
1312
}
1413

15-
console.log(decimalNumber);
14+
console.log(convertToPercentage(0.5));
1615

17-
// =============> write your explanation here
16+
// 'decimalNumber' is already declared in two different areas in the same function which JavaScript does not allow.
17+
// 'decimalNumber' is only useful inside the function, so it will give an error of not knowing what decimalNumber is,
18+
// even if it succeeds it will give the wrong output since what we are looking for is the percentage.
1819

1920
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
21+
// New code: 1. Removed the duplicate `const decimalNumber = 0.5;` line.
22+
// 2. Changed the console.log to call the function and print the result.

0 commit comments

Comments
 (0)