Skip to content

Commit bd416ad

Browse files
committed
Complete Sprint 3 section 2: debug
1 parent ba92053 commit bd416ad

3 files changed

Lines changed: 42 additions & 16 deletions

File tree

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

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

3-
// =============> write your prediction here
3+
// Prediction: The multiplication itself prints 320, but the template string
4+
// then shows undefined because multiply does not return the result.
45

5-
function multiply(a, b) {
6+
function multiplyBeforeFix(a, b) {
67
console.log(a * b);
78
}
89

10+
console.log(`The result of multiplying 10 and 32 is ${multiplyBeforeFix(10, 32)}`);
11+
12+
function multiply(a, b) {
13+
return a * b;
14+
}
15+
916
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1017

11-
// =============> write your explanation here
18+
// Observed before the fix: 320, then "undefined" in the template string.
19+
// Explanation: console.log displays the product but returns undefined. The
20+
// function call inside the template string therefore evaluates to undefined.
1221

1322
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
23+
// The corrected function above returns the product for the template string.

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// Prediction: The output will say undefined because return exits the function
3+
// before the addition expression can run.
34

4-
function sum(a, b) {
5+
function sumBeforeFix(a, b) {
56
return;
67
a + b;
78
}
89

10+
console.log(`The sum of 10 and 32 is ${sumBeforeFix(10, 32)}`);
11+
12+
function sum(a, b) {
13+
return a + b;
14+
}
15+
916
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1017

11-
// =============> write your explanation here
18+
// Observed before the fix: undefined.
19+
// Explanation: A newline immediately after return ends the return statement.
20+
// The function returns undefined and the following expression is unreachable.
1221
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
22+
// The corrected function above returns the sum.

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// Prediction: Each line reports 103's last digit (3), because getLastDigit
5+
// ignores its arguments and always reads the global num variable.
56

67
const num = 103;
78

8-
function getLastDigit() {
9+
function getLastDigitBeforeFix() {
910
return num.toString().slice(-1);
1011
}
1112

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)}`);
13+
console.log(`The last digit of 42 is ${getLastDigitBeforeFix(42)}`);
14+
console.log(`The last digit of 105 is ${getLastDigitBeforeFix(105)}`);
15+
console.log(`The last digit of 806 is ${getLastDigitBeforeFix(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18+
// Output: Each line reports 3.
1819
// Explain why the output is the way it is
19-
// =============> write your explanation here
20+
// Explanation: getLastDigit has no parameter and reads the unchanged global
21+
// num (103), so passing 42, 105, or 806 has no effect.
2022
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
23+
function getLastDigit(number) {
24+
return number.toString().slice(-1);
25+
}
26+
27+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
28+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
29+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2230

2331
// This program should tell the user the last digit of each number.
2432
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)