Skip to content

Commit e120467

Browse files
Complete Sprint-3 debug exercises (0.js, 1.js, 2.js)
1 parent ac7e78b commit e120467

3 files changed

Lines changed: 50 additions & 18 deletions

File tree

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
// Predict and explain first...
2-
3-
// =============> write your prediction here
2+
// ==============> write your prediction here
3+
// I predict this will run without an error, but print the wrong result at
4+
// the end. The 'multiply' function uses console.log internally instead of
5+
// returning a value, so when it's used inside the template literal, it will
6+
// show as 'undefined' instead of the actual multiplication result.
47

58
function multiply(a, b) {
6-
console.log(a * b);
9+
return a * b;
710
}
811

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

11-
// =============> write your explanation here
14+
// ==============> write your explanation here
15+
// The original 'multiply' function printed the result with console.log
16+
// instead of returning it. A function that doesn't explicitly return a value
17+
// returns 'undefined' by default. When the outer console.log tried to use
18+
// multiply(10, 32) inside the template literal, it inserted 'undefined'
19+
// instead of the actual number, because it was using the function's return
20+
// value, not what it printed internally. Changing console.log to return
21+
// inside the function fixes this, since now the function actually hands
22+
// back the calculated value.
1223

1324
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
25+
// ==============> write your new code here
26+
// (the fixed function above already satisfies this)

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// ==============> write your prediction here
3+
// I predict this will run without an error, but the result will show as
4+
// 'undefined' instead of the actual sum. The 'return' statement is on its
5+
// own line, separate from 'a + b;' on the next line — JavaScript will treat
6+
// these as two separate statements, so the function returns immediately
7+
// with nothing, and 'a + b' never actually gets returned.
38

49
function sum(a, b) {
5-
return;
6-
a + b;
10+
return a + b;
711
}
812

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

11-
// =============> write your explanation here
15+
// ==============> write your explanation here
16+
// JavaScript has a feature called Automatic Semicolon Insertion (ASI). When
17+
// 'return' appears on its own line with nothing after it, JavaScript
18+
// automatically treats it as 'return;' — ending the function right there and
19+
// returning 'undefined'. The following line, 'a + b;', becomes dead code
20+
// that never runs, because the function has already exited. This is why the
21+
// original code always produced 'undefined' instead of the sum. The fix is
22+
// to put the expression on the same line as 'return', so JavaScript knows
23+
// it's part of the return statement: 'return a + b;'.
24+
1225
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
26+
// ==============> write your new code here
27+
// (the fixed function above already satisfies this)

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// ==============> write your prediction here
5+
// I predict every call to getLastDigit will return the same result: "3",
6+
// no matter what number is passed in. This is because the function doesn't
7+
// take any parameters — it always uses the outer 'num' variable (103)
8+
// instead of the value passed into the function call.
59

610
const num = 103;
711

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

1620
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
18-
// Explain why the output is the way it is
19-
// =============> write your explanation here
20-
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
21+
// ==============> write the output here
22+
// The last digit of 42 is 3
23+
// The last digit of 105 is 3
24+
// The last digit of 806 is 3
2225

23-
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem
26+
// Explain why the output is the way it is
27+
// ==============> write your explanation here
28+
// The function getLastDigit() is defined with no parameters, so the values
29+
// 42, 105, and 806 passed into each call are simply ignored — they go
30+
// nowhere. Instead, the

0 commit comments

Comments
 (0)