Skip to content

Commit f0d2c5a

Browse files
Fix variable redeclaration and update function call
1 parent 09a867e commit f0d2c5a

File tree

1 file changed

+24
-4
lines changed
  • Sprint-2/1-key-errors

1 file changed

+24
-4
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
// Predict and explain first...
2-
2+
// syntax error identifier decimalNumber be already declare
3+
// there is no function call and then you can't use the local variable decimalNumber out of the function
34
// Why will an error occur when this program runs?
5+
//decimalNumber is already declared as a parameter, so declaring it again inside the function causes an error.
6+
7+
//There is no function call.
8+
9+
//You cannot use the local variable decimalNumber outside the function scope.
410
// =============> write your prediction here
511

612
// Try playing computer with the example to work out what is going on
713

814
function convertToPercentage(decimalNumber) {
9-
const decimalNumber = 0.5;
15+
decimalNumber = 0.5;
1016
const percentage = `${decimalNumber * 100}%`;
1117

1218
return percentage;
1319
}
14-
15-
console.log(decimalNumber);
20+
console.log(convertToPercentage());
1621

1722
// =============> write your explanation here
23+
//The error occurred because a local variable was redeclared inside the function using a variable keyword. Since the parameter already acts as a local variable, redeclaring it caused an error.
24+
25+
//To fix this, I removed the variable keyword and modified the existing parameter instead.
26+
27+
//I also removed the unused variable from console.log because it was outside the function scope.
1828

29+
//Finally, I called the function directly inside console.log using convertToPercentage() so the function executes properly and returns the correct value.
1930
// Finally, correct the code to fix the problem
2031
// =============> write your new code here
32+
function convertToPercentage(decimalNumber) {
33+
34+
const percentage = `${decimalNumber * 100}%`;
35+
36+
return percentage;
37+
}
38+
39+
const decimalNumber = convertToPercentage(0.5) ;
40+
console.log(decimalNumber);

0 commit comments

Comments
 (0)