Skip to content

Commit 19aa42e

Browse files
committed
Merge branch 'Sprint-2/2-mandatory-debug' into Sprint-2/1-key-errors
2 parents 9d22332 + 51a54b4 commit 19aa42e

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

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

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

33
// =============> write your prediction here
4+
// PREDICTION: The function will log the result of the multiplication, but the template literal will not include the result because multiply() does not return a value.
45

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

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

1112
// =============> write your explanation here
1213

14+
// EXPLANATION: The multiply function logs the product of a and b, but it does not return any value. When we try to use multiply(10, 32) inside the template literal, it evaluates to undefined because there is no return statement in the function. Therefore, the output will be "The result of multiplying 10 and 32 is undefined".
1315
// Finally, correct the code to fix the problem
1416
// =============> write your new code here
17+
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: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// PREDICTION: The function will not return the sum because the return statement is placed before the actual addition operation.
34

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

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

1112
// =============> write your explanation here
13+
// The return statement is placed before the addition operation, so the function will return undefined instead of the sum.
14+
1215
// Finally, correct the code to fix the problem
1316
// =============> write your new code here
17+
function sum(a, b) {
18+
return a + b;
19+
}
20+
21+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,38 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// PREDICTION: The function will log the last digit of the number passed to it, but it will not work as intended because the num variable is defined outside the function and is not being passed as an argument.
56

6-
const num = 103;
77

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
8+
// const num = 103;
119

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)}`);
10+
// function getLastDigit() {
11+
// return num.toString().slice(-1);
12+
// }
13+
14+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
19+
1720
// =============> write the output here
21+
// The last digit of 42 is 3
22+
// The last digit of 105 is 3
23+
// The last digit of 806 is 3
1824
// Explain why the output is the way it is
25+
// The output is not as expected because the getLastDigit function is not using the argument passed to it. Instead, it is using the num variable defined outside the function, which is not being updated with the new values.
1926
// =============> write your explanation here
27+
// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function.
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+
}
2233

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)}`);
2337
// This program should tell the user the last digit of each number.
2438
// Explain why getLastDigit is not working properly - correct the problem
39+
// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function.

0 commit comments

Comments
 (0)