File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1111// return str;
1212//}
1313
14-
1514// =============> write your explanation here
1615//"str" has been declared in function. In the local scope of original code, str declared again.
1716//It caused the problem of redeclaration. Delete the "let" in local scope
@@ -21,6 +20,4 @@ function capitalise(str) {
2120 return str ;
2221}
2322
24- console . log ( capitalise ( 'roger' ) )
25-
26-
23+ console . log ( capitalise ( "roger" ) ) ;
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+ // A: There have the problem of redeclaration of the statement of decimalNumber. Local Scope should return to decimalNumber instead.
6+ // To print the code, the code should be console.log(decimalNumber)
57
68// Try playing computer with the example to work out what is going on
79
8- function convertToPercentage ( decimalNumber ) {
9- const decimalNumber = 0.5 ;
10- const percentage = `${ decimalNumber * 100 } %` ;
10+ // function convertToPercentage(decimalNumber) {
11+ // const decimalNumber = 0.5;
12+ // const percentage = `${decimalNumber * 100}%`;
1113
12- return percentage ;
13- }
14+ // return percentage;
1415
15- console . log ( decimalNumber ) ;
16+ // console.log(decimalNumber);
1617
1718// =============> write your explanation here
19+ //A: In the local scope, remove "const decimalNumber = 0.5". for the second line of local scope, 'const percentage' should be change to
20+ //'decimalNumber' to avoid redeclaration and refer the correct variable. and return the function to 'decimalNumber'
21+ //finally console.log(convertToPercentage(0.5)) should be used to print the function.
1822
1923// Finally, correct the code to fix the problem
2024// =============> write your new code here
25+
26+ function convertToPercentage ( decimalNumber ) {
27+ decimalNumber = `${ decimalNumber * 100 } %` ;
28+
29+ return decimalNumber ;
30+ }
31+
32+ console . log ( convertToPercentage ( 0.5 ) ) ;
Load diff This file was deleted.
You can’t perform that action at this time.
0 commit comments