File tree Expand file tree Collapse file tree 1 file changed +9
-7
lines changed
Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change 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
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 ) ;
You can’t perform that action at this time.
0 commit comments