File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed
Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change 22
33// Why will an error occur when this program runs?
44// =============> write your prediction here
5+ // I predict that this program will throw a error message - due to the variable decimalNumber being redeclared
56
67// Try playing computer with the example to work out what is going on
78
@@ -15,6 +16,22 @@ function convertToPercentage(decimalNumber) {
1516console . log ( decimalNumber ) ;
1617
1718// =============> write your explanation here
19+ // Function covertToPercentage(decimalNumber) - it has a parameter named decimalNumber
20+ // Inside the function: const decimalNumber = 0.5; - this redeclares the same variable name that's already used for the parameter.
21+ // That causes a error: SyntaxError: Identifier 'decimalNumber' has already been declared - because you can't declare a const (or let)
22+ // with the same name as a parameter inside the same function scope.
23+ // JavaScript doesn't allow us to declare a new variable with the same name in the same scope, so it caused a error
1824
1925// Finally, correct the code to fix the problem
2026// =============> write your new code here
27+ function convertToPercentage ( decimalNumber ) {
28+ const percentage = `${ decimalNumber * 100 } %` ;
29+ return percentage ;
30+ }
31+
32+ console . log ( convertToPercentage ( 0.5 ) ) ;
33+
34+ // Function decimalNumber = 0.5
35+ // It calculates 0.5 * 100 = 50
36+ // It returns "50%"
37+ // console logs 50%
You can’t perform that action at this time.
0 commit comments