File tree Expand file tree Collapse file tree 3 files changed +37
-10
lines changed
Expand file tree Collapse file tree 3 files changed +37
-10
lines changed Original file line number Diff line number Diff line change 11// Predict and explain first...
2- // =============> write your prediction here
2+ // =============> This code will give an error message
33
44// call the function capitalise with a string input
55// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,10 @@ function capitalise(str) {
99 return str ;
1010}
1111
12- // =============> write your explanation here
13- // =============> write your new code here
12+ // =============> it gave an error because the variable str is being declared twice one with the function and another one with anew variable
13+ // =============> this can be fixed by changing the variable name inside the function
14+ function capitalise ( str ) {
15+ let capitalised = `${ str [ 0 ] . toUpperCase ( ) } ${ str . slice ( 1 ) } ` ;
16+ return capitalised ;
17+ }
18+ console . log ( capitalise ( "sophia" ) ) ; // Output: "Sophia"
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+ // =============> I can see that the variable decimalNumber is being declared twice once as a function parameter and again as a constant
55
66// Try playing computer with the example to work out what is going on
77
@@ -14,7 +14,18 @@ function convertToPercentage(decimalNumber) {
1414
1515console . log ( decimalNumber ) ;
1616
17- // =============> write your explanation here
17+ // =============> so the function is to convert a decimal number into a percentage
18+ // so if the number is 0.5 it is multiplied by 100
19+ // % sign is added at the end to make it 50%
1820
1921// Finally, correct the code to fix the problem
20- // =============> write your new code here
22+ // =============> the problem can be fixed by removing the redeclaration of the variable decimalNumber inside the function
23+ function convertToPercentage ( decimalNumber ) {
24+ const percentage = `${ decimalNumber * 100 } %` ;
25+ return percentage ;
26+ }
27+
28+ // Call the function and store the result in a variable
29+ const result = convertToPercentage ( 0.5 ) ;
30+ console . log ( result ) ;
31+
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+ // =============> First there is a number inside the function which is not allowed in JavaScript it should be a variable name
7+ //the second mistake is num is not declared
78
89function square ( 3 ) {
910 return num * num ;
1011}
1112
12- // =============> write the error message here
13+ // =============> Uncaught SyntaxError: Unexpected number
14+ // =============> we put a number (3)where JavaScript expected a variable name
15+ //Uncaught SyntaxError: Illegal return statement
1316
14- // =============> explain this error message here
17+ //we cant use return inside the function without declaring the function properly
1518
1619// Finally, correct the code to fix the problem
1720
18- // =============> write your new code here
21+
22+ // =============>
23+ function square ( num ) {
24+ return num * num ;
25+ }
26+ console . log ( square ( 3 ) ) ; // the answer should be 9
27+ console . log ( square ( 5 ) ) ; // the answer should be 25
28+ console . log ( square ( 10 ) ) ; // the answer should be 100
29+
1930
2031
You can’t perform that action at this time.
0 commit comments