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 print 320, but the final message will show "undefined".
4
4
5
5
functionmultiply(a,b){
6
-
console.log(a*b);
6
+
returna*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
+
// Explanation: The original function used console.log() instead of return. console.log() displays the result on the screen but does not return a value, so the function returned undefined. Changing console.log() to return fixes the problem.
// Prediction: The function will return undefined because the return statement ends before a + b is executed.
3
4
4
5
functionsum(a,b){
5
-
return;
6
-
a+b;
6
+
returna+b;
7
7
}
8
8
9
9
console.log(`The sum of 10 and 32 is ${sum(10,32)}`);
10
10
11
-
// =============> write your explanation here
11
+
// Explanation: In the original code, JavaScript automatically inserts a semicolon after `return` because it is on its own line. This means the function returns undefined immediately, and `a + b` is never executed.
// The program will print 3 for all three lines because the function always uses the constant 'num', which is 103.
5
6
6
-
constnum=103;
7
-
8
-
functiongetLastDigit(){
7
+
functiongetLastDigit(num){
9
8
returnnum.toString().slice(-1);
10
9
}
11
10
@@ -14,11 +13,17 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
13
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
14
16
15
// Now run the code and compare the output to your prediction
17
-
// =============> write the output here
16
+
// Output:
17
+
// The last digit of 42 is 2
18
+
// The last digit of 105 is 5
19
+
// The last digit of 806 is 6
20
+
18
21
// Explain why the output is the way it is
19
-
// =============> write your explanation here
22
+
// Explanation:
23
+
// The original function always used the constant 'num' (103), so it always returned 3. By giving the function a parameter called 'num', it now uses the value passed into the function each time it is called.
24
+
20
25
// Finally, correct the code to fix the problem
21
26
// =============> write your new code here
22
27
23
28
// This program should tell the user the last digit of each number.
24
-
// Explain why getLastDigit is not working properly - correct the problem
29
+
// Explain why getLastDigit is not working properly - correct the problem.
0 commit comments