You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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
3
4
// 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.
4
10
// =============> write your prediction here
5
11
6
12
// Try playing computer with the example to work out what is going on
7
13
8
14
functionconvertToPercentage(decimalNumber){
9
-
constdecimalNumber=0.5;
15
+
decimalNumber=0.5;
10
16
constpercentage=`${decimalNumber*100}%`;
11
17
12
18
returnpercentage;
13
19
}
14
-
15
-
console.log(decimalNumber);
20
+
console.log(convertToPercentage());
16
21
17
22
// =============> 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.
18
28
29
+
//Finally, I called the function directly inside console.log using convertToPercentage() so the function executes properly and returns the correct value.
0 commit comments