Skip to content

Commit 471439c

Browse files
committed
Fix function implementations to return correct values in multiply, sum, and getLastDigit functions
1 parent e3b695c commit 471439c

File tree

3 files changed

+59
-18
lines changed

3 files changed

+59
-18
lines changed

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

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

33
// =============> write your prediction here
4+
// I predict that there will be error because the function will not return anything
5+
// and when we try to log the result of the function call to the console, it will return undefined.
46

5-
function multiply(a, b) {
6-
console.log(a * b);
7-
}
7+
// function multiply(a, b) {
8+
// console.log(a * b);
9+
// }
810

9-
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
11+
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1012

1113
// =============> write your explanation here
14+
// The function multiply logs in the result of a * b to the console but does not return anything,
15+
// so when we try to log the result of the function call to the console, it will return undefined.
1216

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

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// I predict there will be a syntax error but not sure what the error is yet.
4+
// I think it has something to do with return as it return nothing , so the function was'nt well defined.
35

4-
function sum(a, b) {
5-
return;
6-
a + b;
7-
}
6+
// function sum(a, b) {
7+
// return;
8+
// a + b;
9+
// }
810

9-
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
11+
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1012

1113
// =============> write your explanation here
14+
// The function sum is not returning the result of a + b, instead it is returning undefined
15+
// because the return statement is on a separate line and does not include the expression a + b.
16+
1217
// Finally, correct the code to fix the problem
1318
// =============> write your new code here
19+
20+
function sum(a, b) {
21+
return a + b;
22+
}
23+
24+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
25+

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,42 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// II predict that the output will be "The last digit of 42 is 3", "The last digit of 105 is 3",
6+
// and "The last digit of 806 is 3" because the function getLastDigit is using the variable num
7+
// which is assigned the value of 103 using const in global scope,
8+
// so it will always return the last digit of 103 which is 3.
59

6-
const num = 103;
10+
// const num = 103;
711

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
12+
// function getLastDigit() {
13+
// return num.toString().slice(-1);
14+
// }
1115

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)}`);
16+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
17+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
18+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1519

1620
// Now run the code and compare the output to your prediction
1721
// =============> 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
25+
// it's similar to what I predicted.
26+
1827
// Explain why the output is the way it is
1928
// =============> write your explanation here
29+
// The output is the way it is because the function getLastDigit is using the variable num which is assigned
30+
// the value of 103 using const in global scope, and the function definition didn't have an argument to receive the number.
31+
2032
// Finally, correct the code to fix the problem
2133
// =============> write your new code here
2234

23-
// This program should tell the user the last digit of each number.
24-
// Explain why getLastDigit is not working properly - correct the problem
35+
36+
function getLastDigit() {
37+
return num.toString().slice(-1);
38+
}
39+
40+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
41+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
42+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
43+

0 commit comments

Comments
 (0)