File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree 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- // =============> write your prediction here
4+ // =============> We will get a SyntaxError as decimalNumber has already been declared as a parameter of the function.
55
66// Try playing computer with the example to work out what is going on
77
8+ /*
89function convertToPercentage(decimalNumber) {
910 const decimalNumber = 0.5;
1011 const percentage = `${decimalNumber * 100}%`;
1112
1213 return percentage;
1314}
1415
15- console . log ( decimalNumber ) ;
16+ console.log(decimalNumber); */
1617
1718// =============> write your explanation here
19+ // We don't need to redeclare decimalNumber as it's value comes from function parameter,
20+ // also console.log wont work as decimalNumber is only created inside the function.
1821
1922// Finally, correct the code to fix the problem
2023// =============> write your new code here
24+ function convertToPercentage ( decimalNumber ) {
25+ const percentage = `${ decimalNumber * 100 } %` ;
26+
27+ return percentage ;
28+ }
29+ console . log ( convertToPercentage ( 0.5 ) ) ;
Original file line number Diff line number Diff line change 33
44// this function should square any number but instead we're going to get an error
55
6- // =============> write your prediction of the error here
6+ // =============> SyntaxError because function parameter must be a name not a number or value. The error occurs because 3 is a number.
77
8+ /*
89function square(3) {
910 return num * num;
10- }
11+ } */
1112
12- // =============> write the error message here
13+ // =============> SyntaxError: Unexpected number
1314
14- // =============> explain this error message here
15+ // =============> This error message is cause by trying to assign a number as a parameter, a parameter needs to be identifier such as num.
1516
1617// Finally, correct the code to fix the problem
1718
1819// =============> write your new code here
19-
20+ function square ( num ) {
21+ return num * num ;
22+ }
23+ console . log ( square ( 5 ) )
2024
You can’t perform that action at this time.
0 commit comments