Skip to content

Commit 7861f3a

Browse files
author
lintsang
committed
finished exercises in the 2-mandatory-debug folder
1 parent a5e107e commit 7861f3a

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

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

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

3-
// =============> write your prediction here
3+
// =============> I predict the function will be called when the console try to print the sentence 'The result....'. In this case, two values 10 and 32 are inserted to this function. And a result will be calculated '10x32'. But I expected the function won't work exactly what people expect because I expect the result will be return instead of console print it to me. If I change this, I will expect the code works then.
44

55
function multiply(a, b) {
66
console.log(a * b);
77
}
88

99
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
1010

11-
// =============> write your explanation here
11+
// =============> When I run this code, the sentence 'The result of multiplying 10 and 32 is undefined' comes to me. I think it is caused by the missed return statement and no value will be returned to this result in the sentence.
1212

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

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
// =============> write your prediction here
2+
// =============> I predict an error will occur as the line 5 and 6 seems not to be a correct syntax to return a value that the function needs to.
33

44
function sum(a, b) {
55
return;
@@ -8,6 +8,10 @@ function sum(a, b) {
88

99
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1010

11-
// =============> write your explanation here
11+
// =============> When I run the code, 'The sum of 10 and 32 is undefined' comes, it is because the line 5 and 6 need to be merge to properly return a value.
1212
// Finally, correct the code to fix the problem
13-
// =============> write your new code here
13+
// =============> function sum(a, b) {
14+
// =============> return a + b;
15+
// =============> }
16+
17+
// =============> console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> I predict the code function in this file will extract the last digit of number for us. The function itself seems working fine and the function call statement should work fine too. But the issue is that there is a declaration of variable 'num' on line 6 which will fix all the input to the function statement so we cannot expect the correct return value in this code.
55

66
const num = 103;
77

@@ -14,11 +14,18 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
17+
// =============> As expected, all the return value is '3' for the function with difference parameter called.
1818
// Explain why the output is the way it is
19-
// =============> write your explanation here
19+
// =============> As the declared variable 'num' has been assigned with a fixed value.
2020
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
21+
// =============> function getLastDigit(num) {
22+
// =============> return num.toString().slice(-1);
23+
// =============> }
24+
25+
// =============> console.log(`The last digit of 42 is ${getLastDigit(42)}`);
26+
// =============> console.log(`The last digit of 105 is ${getLastDigit(105)}`);
27+
// =============> console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2228

2329
// This program should tell the user the last digit of each number.
2430
// Explain why getLastDigit is not working properly - correct the problem
31+
//The overall syntax in this code works fine and it trigger no error. But the code doesn't work properly and doesn't show the digit we expected is because it tried to declare the num with a fixed number in the beginning of code instead of putting it in the parenthesis of function. It makes the parameter cannot be successfully pass to the function, so the outcome will not be right.

0 commit comments

Comments
 (0)