Skip to content

Commit 99d16a4

Browse files
committed
completed tasks in 2-mandatory-debug/2.js
1 parent 4ca8651 commit 99d16a4

File tree

1 file changed

+20
-7
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+20
-7
lines changed

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

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

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// The console logs will all be show 3 for the function call because num used in the function is declared before and without a parameter the function will always return the last digit of num.
56

6-
const num = 103;
7+
// 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
1822
// Explain why the output is the way it is
1923
// =============> write your explanation here
24+
// The output is the way it is because the function getLastDigit does not take any parameters and always uses the global variable num which is set to 103. So all the function calls return the same result.
2025
// Finally, correct the code to fix the problem
2126
// =============> write your new code here
2227

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

0 commit comments

Comments
 (0)