File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
Expand file tree Collapse file tree 1 file changed +19
-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+ // the code will throw an error because the variable str is being declared twice within the same scope
56
67// Try playing computer with the example to work out what is going on
78
9+ /*
10+
811function convertToPercentage(decimalNumber) {
912 const decimalNumber = 0.5;
1013 const percentage = `${decimalNumber * 100}%`;
@@ -14,7 +17,23 @@ function convertToPercentage(decimalNumber) {
1417
1518console.log(decimalNumber);
1619
20+ */
21+
1722// =============> write your explanation here
23+ /* The error occurs because the variable 'decimalNumber' is declared twice within the same function scope using 'const'.
24+ This is not allowed in JavaScript, as each variable must have a unique name within its scope. When the function is called,
25+ the JavaScript engine encounters the second declaration of 'decimalNumber' and throws a SyntaxError indicating that the
26+ identifier has already been declared.*/
1827
1928// Finally, correct the code to fix the problem
2029// =============> write your new code here
30+
31+ let decimalNumber = 0.6 ;
32+
33+ function convertToPercentage ( decimalNumber ) {
34+ const percentage = `${ decimalNumber * 100 } %` ;
35+
36+ return percentage ;
37+ }
38+
39+ console . log ( convertToPercentage ( decimalNumber ) ) ;
You can’t perform that action at this time.
0 commit comments