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
// PREDICTION: The function will log the result of the multiplication, but the template literal will not include the result because multiply() does not return a value.
4
5
5
-
functionmultiply(a,b){
6
-
console.log(a*b);
7
-
}
6
+
// function multiply(a, b) {
7
+
// console.log(a * b);
8
+
// }
8
9
9
-
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
10
+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10
11
11
12
// =============> write your explanation here
12
13
14
+
// EXPLANATION: The multiply function logs the product of a and b, but it does not return any value. When we try to use multiply(10, 32) inside the template literal, it evaluates to undefined because there is no return statement in the function. Therefore, the output will be "The result of multiplying 10 and 32 is undefined".
13
15
// Finally, correct the code to fix the problem
14
16
// =============> write your new code here
17
+
18
+
functionmultiply(a,b){
19
+
returna*b;
20
+
}
21
+
22
+
console.log(`The result of multiplying 10 and 32 is ${multiply(10,32)}`);
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+22-7Lines changed: 22 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -2,23 +2,38 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
+
// PREDICTION: The function will log the last digit of the number passed to it, but it will not work as intended because the num variable is defined outside the function and is not being passed as an argument.
5
6
6
-
constnum=103;
7
7
8
-
functiongetLastDigit(){
9
-
returnnum.toString().slice(-1);
10
-
}
8
+
// const num = 103;
11
9
12
-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13
-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
10
+
// function getLastDigit() {
11
+
// return num.toString().slice(-1);
12
+
// }
13
+
14
+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15
+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16
+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
17
16
18
// Now run the code and compare the output to your prediction
19
+
17
20
// =============> write the output here
21
+
// The last digit of 42 is 3
22
+
// The last digit of 105 is 3
23
+
// The last digit of 806 is 3
18
24
// Explain why the output is the way it is
25
+
// The output is not as expected because the getLastDigit function is not using the argument passed to it. Instead, it is using the num variable defined outside the function, which is not being updated with the new values.
19
26
// =============> write your explanation here
27
+
// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function.
20
28
// Finally, correct the code to fix the problem
21
29
// =============> write your new code here
30
+
functiongetLastDigit(num){
31
+
returnnum.toString().slice(-1);
32
+
}
22
33
34
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
35
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
36
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
23
37
// This program should tell the user the last digit of each number.
24
38
// Explain why getLastDigit is not working properly - correct the problem
39
+
// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function.
0 commit comments