Skip to content

Commit 0f13007

Browse files
committed
Completed exercise 2 fully
1 parent 21acb1b commit 0f13007

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
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+
// The function multiply does not return any value, it only logs the result to the console.
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
13+
// The function multiply does not return any value, it only logs the result to the console.
14+
// When we try to use the result of multiply(10, 32), it will be undefined.
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: 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+
// The function sum does not return any value because of the misplaced semicolon after return.
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 function sum has a return statement followed by a semicolon, which causes the function to return undefined.
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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,39 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// The function will always return '3' because it uses the global variable num instead of the input parameter.
56

67
const num = 103;
78

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
9+
// function getLastDigit() {
10+
// return num.toString().slice(-1);
11+
// }
1112

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)}`);
13+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
14+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
15+
// 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 last digit of 42 is 3
20+
// The last digit of 105 is 3
21+
// The last digit of 806 is 3
22+
1823
// Explain why the output is the way it is
1924
// =============> write your explanation here
25+
// The function getLastDigit does not use its input parameter. Instead it always converts the global
26+
// variable num, which is 103 to a string and returns its last character which is '3'.
27+
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
2230

31+
function getLastDigit(number) {
32+
return number.toString().slice(-1);
33+
}
34+
35+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
36+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
37+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
38+
2339
// This program should tell the user the last digit of each number.
2440
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)