Skip to content

Commit bb7033c

Browse files
committed
completed the 2-mandatory-debug of sprint-2
1 parent 2a0a2b7 commit bb7033c

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

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

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

33
// =============> write your prediction here
4+
// An error will occur because the function 'multiply' does not return any value. When we try to use the result of 'multiply(10, 32)' in the template literal, it will be 'undefined', which is not the expected output. To fix this, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'.
45

5-
function multiply(a, b) {
6+
/* function multiply(a, b) {
67
console.log(a * b);
78
}
89
910
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
11+
*/
1012

1113
// =============> write your explanation here
14+
// The error is occurring because the 'multiply' function does not return any value. When we call 'multiply(10, 32)', it executes the console.log statement inside the function, which prints the product of 'a' and 'b' to the console. However, since there is no return statement, the function returns 'undefined' by default. When we try to use this 'undefined' value in the template literal, it does not give us the expected output. To fix this issue, we need to add a return statement in the 'multiply' function to return the product of 'a' and 'b'.
1215

1316
// Finally, correct the code to fix the problem
1417
// =============> write your new code here
18+
function multiply(a, b) {
19+
return a * b;
20+
}
21+
22+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// An error will occur because the 'sum' function does not return any value. When we try to use the result of 'sum(10, 32)' in the template literal, it will be 'undefined', which is not the expected output. To fix this, we need to add a return statement in the 'sum' function to return the sum of 'a' and 'b'.
34

4-
function sum(a, b) {
5+
/* function sum(a, b) {
56
return;
67
a + b;
78
}
9+
810
911
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
12+
*/
1013

1114
// =============> write your explanation here
15+
// The error is occurring because the 'sum' function does not return any value. When we call 'sum(10, 32)', it executes the return statement without any value, which means the function returns 'undefined' by default. When we try to use this 'undefined' value in the template literal, it does not give us the expected output. To fix this issue, we need to change the return statement to return the sum of 'a' and 'b', like this: 'return a + b;'.
1216
// Finally, correct the code to fix the problem
1317
// =============> write your new code here
18+
function sum(a, b) {
19+
return a + b;
20+
}
21+
22+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

34
// Predict the output of the following code:
45
// =============> Write your prediction here
6+
// The output will be:
7+
// The last digit of 42 is 3
8+
// The last digit of 105 is 3
9+
// The last digit of 806 is 3
510

11+
/*
612
const num = 103;
713
814
function getLastDigit() {
@@ -12,13 +18,25 @@ function getLastDigit() {
1218
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1319
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1420
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
21+
*/
1522

1623
// Now run the code and compare the output to your prediction
1724
// =============> write the output here
25+
// The output is:
26+
// The last digit of 42 is 3
27+
// The last digit of 105 is 3
28+
// The last digit of 806 is 3
1829
// Explain why the output is the way it is
1930
// =============> write your explanation here
31+
// The output is the way it is because the 'getLastDigit' function is not using the input parameter to calculate the last digit. Instead, it is always converting the global variable 'num' (which is set to 103) to a string and slicing the last character, which results in '3'. To fix this issue, we need to modify the 'getLastDigit' function to accept a parameter and use that parameter instead of the global variable 'num'. For example:
2032
// Finally, correct the code to fix the problem
2133
// =============> write your new code here
34+
function getLastDigit(number) {
35+
return number.toString().slice(-1);
36+
}
37+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
38+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
39+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2240

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

0 commit comments

Comments
 (0)