Skip to content

Commit 4ccd588

Browse files
committed
2-mandatory-debug Done
1 parent 80d9aff commit 4ccd588

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Predict and explain first...
2-
32
// =============> write your prediction here
3+
// this code will use the declared function multiplay (a, b) to multiply two numbers, 10 and 32, and print the result to the console.
4+
// However, the function can not be used to get the result because it does not return any value. Instead, it only logs the result to the console.
45

56
function multiply(a, b) {
67
console.log(a * b);
@@ -9,6 +10,13 @@ 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 function does not return any value, it only prints the result to the console, so it can't be used to return anything, it will return "undefined".
1214

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Predict and explain first...
22
// =============> write your prediction here
3+
// the function will throw an error because it does not return any value, the use of return without passing any value will cause an error.
4+
// also the last line of the function is unreachable because it comes after the return statement.
35

46
function sum(a, b) {
57
return;
@@ -9,5 +11,13 @@ function sum(a, b) {
911
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
1012

1113
// =============> write your explanation here
14+
// The function will not return the expected sum because the return statement is not followed by any value.
15+
1216
// Finally, correct the code to fix the problem
1317
// =============> write your new code here
18+
19+
function sum(a, b) {
20+
return a + b;
21+
}
22+
23+
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
// Predict and explain first...
2+
// the code should display the last digit of any number passed to the function.
3+
// but there is an error which is the variable num is globally declared and has a value of 103.
4+
// the function does not take any parameters, and it takes the global variable num which is always 103.
5+
// that causes the code to always return 3 as 3 is the last digit of 103 which is the value of the global variable num.
26

37
// Predict the output of the following code:
48
// =============> Write your prediction here
9+
// since 3 is the last digit of 103, I think 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
513

614
const num = 103;
715

@@ -14,11 +22,29 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1422
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1523

1624
// Now run the code and compare the output to your prediction
25+
// The last digit of 42 is 3 ... the output is as I predicted.
26+
1727
// =============> write the output here
28+
// The last digit of 42 is 3
29+
// The last digit of 105 is 3
30+
// The last digit of 806 is 3
31+
1832
// Explain why the output is the way it is
1933
// =============> write your explanation here
34+
// because the function getLastDigit does not take any parameters, and it uses the global variable num which is always 103.
2035
// Finally, correct the code to fix the problem
2136
// =============> write your new code here
2237

38+
// no need to declare the global variable num here. instead, we will pass it as a parameter to the function.
39+
function getLastDigit(num) {
40+
return num.toString().slice(-1);
41+
}
42+
43+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
44+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
45+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
46+
47+
2348
// This program should tell the user the last digit of each number.
2449
// Explain why getLastDigit is not working properly - correct the problem
50+
// because the function does not take the parameter so it will always use the variable num which is always 103.

0 commit comments

Comments
 (0)