22
33// Why will an error occur when this program runs?
44// =============> write your prediction here
5+ // Answer: I will get a Syntax Error, because the variable decimalNumber is a parameter
6+ // of the function, and then it gets re-declared inside the function. Removing the
7+ // "const" would make sure it's not re-declared, just value reassigned on that line.
58
69// Try playing computer with the example to work out what is going on
710
11+ /*
812function convertToPercentage(decimalNumber) {
913 const decimalNumber = 0.5;
1014 const percentage = `${decimalNumber * 100}%`;
@@ -13,8 +17,26 @@ function convertToPercentage(decimalNumber) {
1317}
1418
1519console.log(decimalNumber);
20+ */
1621
1722// =============> write your explanation here
23+ // Answer: I got SyntaxError: Identifier 'decimalNumber' has already been declared
24+ // I will make sure it's not re-declared in the function by removing the "const"
25+
26+ // After removing the "const" I got a new error. ReferenceError: decimalNumber is not defined. I realized
27+ // that the console.log at the bottom didn't call the function but just logged decimalNumber which hadn't
28+ // been defined in the global scope. Putting back the "const", I will move the declaration of decimalNumber
29+ // to outside the function to make the scope global. Now the global decimalNumber has nothing to do with the
30+ // local parameter decimalNumber in the function.
1831
1932// Finally, correct the code to fix the problem
2033// =============> write your new code here
34+ function convertToPercentage ( decimalNumber ) {
35+ const percentage = `${ decimalNumber * 100 } %` ;
36+
37+ return percentage ;
38+ }
39+
40+ const decimalNumber = 0.5 ;
41+
42+ console . log ( decimalNumber ) ;
0 commit comments