Skip to content

Commit c09112f

Browse files
committed
Refactor functions to return values instead of logging; update explanations and predictions in comments
1 parent 9659871 commit c09112f

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
// Predict and explain first...
2-
2+
/*
3+
The function 'multiply' does not return any value, it only logs the product of 'a' and 'b' to the console, within itself.
4+
Therefore, when we try to use its result in a template literal, it will be 'undefined'.
5+
In the below, i expect the TWO outputs to be, 320 and The result of multiplying 10 and 32 is undefined.
6+
*/
37
// =============> write your prediction here
4-
8+
/*
59
function multiply(a, b) {
610
console.log(a * b);
711
}
812
913
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10-
14+
*/
1115
// =============> write your explanation here
12-
16+
/*
17+
1. I will change the function to return the product of 'a' and 'b' instead of logging it.
18+
This way, when we call 'multiply(10, 32)', it will return 320,
19+
which can then be used in the template literal.
20+
*/
1321
// Finally, correct the code to fix the problem
1422
// =============> write your new code here
23+
function multiply(a, b) {
24+
return a * b;
25+
}
26+
27+
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
32

3+
// =============> write your prediction here
4+
/*
5+
The function 'sum' does not return the sum of 'a' and 'b' because there is a semicolon immediately after the 'return' statement.
6+
This causes the function to return 'undefined' instead of the intended sum.
7+
*/
8+
/*
49
function sum(a, b) {
510
return;
611
a + b;
712
}
813
914
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
10-
15+
*/
1116
// =============> write your explanation here
17+
/*
18+
1. In JavaScript, when a 'return' statement is followed by a newline, it is treated as if there is a semicolon immediately after 'return'.
19+
2. This means that the function exits at the 'return' statement and does not execute the next line, which contains the actual sum operation.
20+
3. As a result, the function returns 'undefined' instead of the sum of 'a' and 'b'.
21+
4. To fix this, we need to place the expression to be returned on the same line as the 'return' statement.
22+
*/
1223
// Finally, correct the code to fix the problem
1324
// =============> write your new code here
25+
function sum(a, b) {
26+
return a + b;
27+
}
28+
29+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
// Predict and explain first...
2+
/*
3+
The function 'getLastDigit' is not working properly because it does not take any parameters, yet it is being called with arguments (42, 105, 806).
4+
As a result, it always returns the last digit of the constant 'num', which is 103, instead of the last digit of the numbers passed to it.
5+
*/
6+
// =============> write your prediction here
27

8+
/*
9+
The output will be:
10+
The last digit of 42 is 3
11+
The last digit of 105 is 3
12+
The last digit of 806 is 3
13+
*/
314
// Predict the output of the following code:
415
// =============> Write your prediction here
5-
16+
/*
617
const num = 103;
718
819
function getLastDigit() {
@@ -12,13 +23,32 @@ function getLastDigit() {
1223
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1324
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1425
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
26+
*/
1627
// Now run the code and compare the output to your prediction
1728
// =============> write the output here
29+
/*
30+
The output is:
31+
The last digit of 42 is 3
32+
The last digit of 105 is 3
33+
The last digit of 806 is 3
34+
*/
1835
// Explain why the output is the way it is
36+
/* 1. The function 'getLastDigit' does not accept any parameters, so it does not use the values (42, 105, 806) passed to it when called.
37+
2. Instead, it always operates on the constant 'num', which is set to 103.
38+
3. The function converts 'num' to a string and retrieves the last character using 'slice(-1)', which is '3'.
39+
4. Therefore, regardless of the input values, the function always returns '3', leading to the same output for all calls.
40+
*/
1941
// =============> write your explanation here
2042
// Finally, correct the code to fix the problem
2143
// =============> write your new code here
2244

2345
// This program should tell the user the last digit of each number.
2446
// Explain why getLastDigit is not working properly - correct the problem
47+
48+
function getLastDigit(num) {
49+
return num.toString().slice(-1);
50+
}
51+
52+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
53+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
54+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

0 commit comments

Comments
 (0)