Skip to content

Commit fda7742

Browse files
committed
mandatory-debug tasks are done
1 parent 1ff607f commit fda7742

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

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

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

3-
// =============> write your prediction here
3+
// =============> Prediction:
4+
// The program will print "The result of multiplying 10 and 32 is undefined"
5+
// because the function multiply does not return a value, it only prints to the console.
46

57
function multiply(a, b) {
68
console.log(a * b);
79
}
810

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

11-
// =============> write your explanation here
13+
// =============> Explanation:
14+
// 1. The function multiply(a, b) uses console.log inside the function, but it does not return anything.
15+
// 2. When we use ${multiply(10, 32)} inside the string, it tries to use the return value of multiply.
16+
// 3. Since there is no return, JavaScript treats it as undefined.
1217

13-
// Finally, correct the code to fix the problem
14-
// =============> write your new code here
18+
// =============> Corrected Code:
19+
// We need the function to return the result instead of printing it.
20+
21+
function multiply(a, b) {
22+
return a * b;
23+
}
24+
25+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
26+
// Output: "The result of multiplying 10 and 32 is 320"

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
3+
// =============> Prediction:
4+
// The program will print "The sum of 10 and 32 is undefined"
5+
// because the function returns nothing due to the 'return;' line.
36

47
function sum(a, b) {
58
return;
@@ -8,6 +11,17 @@ function sum(a, b) {
811

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

11-
// =============> write your explanation here
12-
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
14+
// =============> Explanation:
15+
// 1. In JavaScript, as soon as the function hits 'return;' it stops and returns undefined.
16+
// 2. The line 'a + b;' is never reached because it's after the return statement.
17+
// 3. Therefore, the function does not actually return the sum.
18+
19+
// =============> Corrected Code:
20+
// We need to return the sum directly.
21+
22+
function sum(a, b) {
23+
return a + b;
24+
}
25+
26+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
27+
// Output: "The sum of 10 and 32 is 42"

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
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:
6+
// The last digit of 42 is 3
7+
// The last digit of 105 is 3
8+
// The last digit of 806 is 3
9+
// because the function always uses the constant 'num' (103) and ignores the numbers we pass.
510

611
const num = 103;
712

@@ -13,12 +18,4 @@ console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1318
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1419
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1520

16-
// 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
22-
23-
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem
21+
// ===========

0 commit comments

Comments
 (0)