Skip to content

Commit 17b2099

Browse files
committed
fixed return statements and added parameter to function
1 parent 2467b5f commit 17b2099

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

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

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

33
// =============> write your prediction here
4+
// The error will occur because the function 'multiply' does not return a value, so when we try to use it will return 'undefined'.
45

56
function multiply(a, b) {
67
console.log(a * b);
@@ -9,6 +10,12 @@ function multiply(a, b) {
910
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// The error occurs because the 'multiply' function does not have a return statement, so it returns 'undefined' by default.
1214

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// It will return 'undefined'.
34

45
function sum(a, b) {
56
return;
@@ -9,5 +10,11 @@ function sum(a, b) {
910
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1011

1112
// =============> write your explanation here
13+
// The error occurs because the 'sum' function has a return statement without a value, so it returns 'undefined' by default.
1214
// Finally, correct the code to fix the problem
1315
// =============> write your new code here
16+
function sum(a, b) {
17+
return a + b;
18+
}
19+
20+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//The output will be 3 in each expression.
56

67
const num = 103;
78

@@ -15,10 +16,28 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
1718
// =============> write the output here
19+
// The output is:
20+
// The last digit of 42 is 3
21+
// The last digit of 105 is 3
22+
// The last digit of 806 is 3
23+
1824
// Explain why the output is the way it is
1925
// =============> write your explanation here
26+
// The function `getLastDigit` is not taking any arguments, so it always returns the last digit which is 103.
27+
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
30+
function getLastDigit(num) {
31+
return num.toString().slice(-1);
32+
}
33+
34+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
35+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
36+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2237

2338
// This program should tell the user the last digit of each number.
2439
// Explain why getLastDigit is not working properly - correct the problem
40+
41+
// The function `getLastDigit` is not working properly because it was using `num`,
42+
// instead of the parameter passed to the function. By changing the function to accept a parameter `num`,
43+
// it now correctly returns the last digit of the number passed to it..

0 commit comments

Comments
 (0)