Skip to content

Commit 522a6de

Browse files
committed
Complete Sprint 2 mandatory debug
1 parent 3e0f905 commit 522a6de

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

Sprint-2/2-mandatory-debug/0.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Predict and explain first...
22

3-
// =============> write your prediction here
3+
// Prediction: The function will print 320, but the final message will show "undefined".
44

55
function multiply(a, b) {
6-
console.log(a * b);
6+
return a * b;
77
}
88

99
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1010

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.
1212

1313
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
14+
// =============> write your new code here

Sprint-2/2-mandatory-debug/1.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
3+
// Prediction: The function will return undefined because the return statement ends before a + b is executed.
34

45
function sum(a, b) {
5-
return;
6-
a + b;
6+
return a + b;
77
}
88

99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

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.
12+
1213
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
14+
// =============> write your new code here

Sprint-2/2-mandatory-debug/2.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// Prediction:
5+
// The program will print 3 for all three lines because the function always uses the constant 'num', which is 103.
56

6-
const num = 103;
7-
8-
function getLastDigit() {
7+
function getLastDigit(num) {
98
return num.toString().slice(-1);
109
}
1110

@@ -14,11 +13,17 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1413
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1514

1615
// 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+
1821
// 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+
2025
// Finally, correct the code to fix the problem
2126
// =============> write your new code here
2227

2328
// 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

Comments
 (0)