Skip to content

Commit d8ecf89

Browse files
Fix syntax error in convertToPercentage function
Updated the explanation for the error and corrected the function declaration to avoid syntax errors.
1 parent 489671d commit d8ecf89

File tree

1 file changed

+9
-7
lines changed
  • Sprint-2/1-key-errors

1 file changed

+9
-7
lines changed

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

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

33
// Why will an error occur when this program runs?
4-
// =============> It will likely be a reference error because the variable decimalNumber is not defined in the global scope
5-
// and is only declared within the function convertToPercentage. When we try to log decimalNumber outside of the function,
6-
// it will not be accessible and will throw an error indicating that decimalNumber is not defined.
4+
// =============> The error occurs because the variable `decimalNumber` is declared twice in the function `convertToPercentage`.
5+
// The first declaration is in the function parameter, and the second declaration is inside the function body.
6+
// This causes a conflict and results in a syntax error.
77

88
// Try playing computer with the example to work out what is going on
99

@@ -16,15 +16,17 @@
1616

1717
//console.log(decimalNumber);
1818

19-
// =============> write your explanation here
19+
// =============> when you run this code it will start by executing the console.log statement and it will look for the variable `decimalNumber`;
20+
// since it is already declared we will get a syntax error and the program will stop executing.
2021

2122
// Finally, correct the code to fix the problem
2223
// =============> write your new code here
2324

24-
function convertToPercentage(a) {
25-
const decimalNumber = 0.5;
25+
function convertToPercentage(decimalNumber) {
2626
const percentage = `${decimalNumber * 100}%`;
2727

2828
return percentage;
2929
}
30-
console.log(convertToPercentage(0.5));
30+
31+
const result = convertToPercentage(0.5);
32+
console.log(result);

0 commit comments

Comments
 (0)