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
// An error will occur because the function 'multiply' does not return any value. When we try to use the result of 'multiply(10, 32)' in the template literal, it will be 'undefined', which is not the expected output. To fix this, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'.
4
5
5
-
functionmultiply(a,b){
6
+
/* function multiply(a, b) {
6
7
console.log(a * b);
7
8
}
8
9
9
10
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
11
+
*/
10
12
11
13
// =============> write your explanation here
14
+
// The error is occurring because the 'multiply' function does not return any value. When we call 'multiply(10, 32)', it executes the console.log statement inside the function, which prints the product of 'a' and 'b' to the console. However, since there is no return statement, the function returns 'undefined' by default. When we try to use this 'undefined' value in the template literal, it does not give us the expected output. To fix this issue, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'.
12
15
13
16
// Finally, correct the code to fix the problem
14
17
// =============> write your new code here
18
+
functionmultiply(a,b){
19
+
returna*b;
20
+
}
21
+
22
+
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
// An error will occur because the 'sum' function does not return any value. When we try to use the result of 'sum(10, 32)' in the template literal, it will be 'undefined', which is not the expected output. To fix this, we need to add a return statement in the 'sum' function to return the sum of 'a' and 'b'.
3
4
4
-
functionsum(a,b){
5
+
/* function sum(a, b) {
5
6
return;
6
7
a + b;
7
8
}
9
+
8
10
9
11
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
12
+
*/
10
13
11
14
// =============> write your explanation here
15
+
// The error is occurring because the 'sum' function does not return any value. When we call 'sum(10, 32)', it executes the return statement without any value, which means the function returns 'undefined' by default. When we try to use this 'undefined' value in the template literal, it does not give us the expected output. To fix this issue, we need to change the return statement to return the sum of 'a' and 'b', like this: 'return a + b;'.
12
16
// Finally, correct the code to fix the problem
13
17
// =============> write your new code here
18
+
functionsum(a,b){
19
+
returna+b;
20
+
}
21
+
22
+
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
+18Lines changed: 18 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,14 @@
1
1
// Predict and explain first...
2
+
//
2
3
3
4
// Predict the output of the following code:
4
5
// =============> Write your prediction here
6
+
// The output will be:
7
+
// The last digit of 42 is 3
8
+
// The last digit of 105 is 3
9
+
// The last digit of 806 is 3
5
10
11
+
/*
6
12
const num = 103;
7
13
8
14
function getLastDigit() {
@@ -12,13 +18,25 @@ function getLastDigit() {
12
18
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13
19
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
20
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
21
+
*/
15
22
16
23
// Now run the code and compare the output to your prediction
17
24
// =============> write the output here
25
+
// The output is:
26
+
// The last digit of 42 is 3
27
+
// The last digit of 105 is 3
28
+
// The last digit of 806 is 3
18
29
// Explain why the output is the way it is
19
30
// =============> write your explanation here
31
+
// The output is the way it is because the 'getLastDigit' function is not using the input parameter to calculate the last digit. Instead, it is always converting the global variable 'num' (which is set to 103) to a string and slicing the last character, which results in '3'. To fix this issue, we need to modify the 'getLastDigit' function to accept a parameter and use that parameter instead of the global variable 'num'. For example:
20
32
// Finally, correct the code to fix the problem
21
33
// =============> write your new code here
34
+
functiongetLastDigit(number){
35
+
returnnumber.toString().slice(-1);
36
+
}
37
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
38
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
39
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
22
40
23
41
// This program should tell the user the last digit of each number.
24
42
// Explain why getLastDigit is not working properly - correct the problem
0 commit comments