Skip to content

Commit 8d3eb4a

Browse files
committed
code error explained and rewritten
1 parent d17d9f0 commit 8d3eb4a

File tree

1 file changed

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

1 file changed

+17
-8
lines changed

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

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

33
// Why will an error occur when this program runs?
4-
// =============> write your prediction here
4+
// constant decimalNumber is being declared twice within the same scope, which will cause a syntax error.
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}%`;
8+
//function convertToPercentage(decimalNumber) {
9+
//const decimalNumber = 0.5;
10+
//const percentage = `${decimalNumber * 100}%`;
1111

12-
return percentage;
13-
}
12+
//return percentage;
13+
//}
1414

15-
console.log(decimalNumber);
15+
//console.log(decimalNumber);
1616

1717
// =============> write your explanation here
18+
// SyntaxError: Identifier 'decimalNumber' has already been declared
19+
// decimalNumber is declared as a parameter of the function and then again inside the function using const.
20+
// In JavaScript, you cannot declare the same variable name in the same scope using let or const.
21+
// Function input is ignored if a variable with the same name is declared inside the function body.
22+
// const decimalNumber = 0.5 will always output 50% regardless of the input value.
1823

1924
// Finally, correct the code to fix the problem
20-
// =============> write your new code here
25+
function convertToPercentage(decimalNumber) {
26+
const percentage = `${decimalNumber * 100}%`;
27+
return percentage;
28+
}
29+
console.log(convertToPercentage(0.75)); // Example call to test the function

0 commit comments

Comments
 (0)