You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// =============> code will produce a SyntaxError the error will occur because the parameter str is being declared again with let str inside the function, which is not allowed
3
3
4
4
// call the function capitalise with a string input
5
5
// interpret the error message and figure out why an error is occurring
@@ -9,5 +9,10 @@ function capitalise(str) {
9
9
returnstr;
10
10
}
11
11
12
-
// =============> write your explanation here
13
-
// =============> write your new code here
12
+
// =============> The error message says that str has already been declared this happens because str is already the function parameter,and the code tries to create another variable called str using let JavaScript does not allow a parameter and a let variable to have the same name in the same scope
Copy file name to clipboardExpand all lines: Sprint-2/1-key-errors/2.js
+8-5Lines changed: 8 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -3,18 +3,21 @@
3
3
4
4
// this function should square any number but instead we're going to get an error
5
5
6
-
// =============> write your prediction of the error here
6
+
// =============> will cause a SyntaxError because function parameters must be variable names, not values like 3
7
7
8
8
functionsquare(3){
9
9
returnnum*num;
10
10
}
11
11
12
-
// =============> write the error message here
12
+
// =============> SyntaxError: Unexpected number
13
13
14
-
// =============> explain this error message here
14
+
// =============> This error happens because 3 is not a valid parameter name in JavaScript function parameters must be variable names like num, not fixed values num is used inside the function but was never defined, so JavaScript throws an error
// =============> will show undefined because the function does not return a value
4
4
5
5
functionmultiply(a,b){
6
6
console.log(a*b);
7
7
}
8
8
9
9
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
10
10
11
-
// =============> write your explanation here
11
+
// =============> the function only uses console.log() so it prints the result but does not return it when a function has no return, JavaScript returns undefined,which is why undefined appears in the final sentence
12
12
13
13
// Finally, correct the code to fix the problem
14
-
// =============> write your new code here
14
+
// =============>
15
+
functionmultiply(a,b){
16
+
returna*b;
17
+
}
18
+
19
+
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
// =============> The sum of 10 and 32 is undefined because the function returns nothing
3
3
4
4
functionsum(a,b){
5
5
return;
@@ -8,6 +8,11 @@ function sum(a, b) {
8
8
9
9
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
10
11
-
// =============> write your explanation here
11
+
// =============> The problem is that the semicolon after return ends the function immediately. This means that a + b never runs, so the function returns undefined instead of the sum
12
12
// Finally, correct the code to fix the problem
13
13
// =============> write your new code here
14
+
functionsum(a,b){
15
+
returna+b;
16
+
}
17
+
18
+
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+13-3Lines changed: 13 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
// Predict and explain first...
2
2
3
3
// Predict the output of the following code:
4
-
// =============> Write your prediction here
4
+
// =============> all three lines will output 3 as the last digit,even though the numbers are different
5
5
6
6
constnum=103;
7
7
@@ -14,11 +14,21 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
14
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
15
16
16
// Now run the code and compare the output to your prediction
17
-
// =============> write the output here
17
+
// =============> The last digit of 42 is 3
18
+
// The last digit of 105 is 3
19
+
// The last digit of 806 is 3
18
20
// Explain why the output is the way it is
19
-
// =============> write your explanation here
21
+
// =============> The function does not use the numbers passed into it instead, it always uses the variable num which is set to 103. The last digit of 103 is 3, so the function always returns 3
20
22
// Finally, correct the code to fix the problem
21
23
// =============> write your new code here
22
24
25
+
functiongetLastDigit(num){
26
+
returnnum.toString().slice(-1);
27
+
}
28
+
29
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
30
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
31
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
32
+
23
33
// This program should tell the user the last digit of each number.
24
34
// Explain why getLastDigit is not working properly - correct the problem
0 commit comments