Skip to content

Commit ebaaadc

Browse files
committed
Predict and explain each function call’s output, then fix the code to produce the expected results.
1 parent f59d4fc commit ebaaadc

File tree

1 file changed

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

1 file changed

+27
-7
lines changed

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

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

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// Prediction
6+
// When the function is called, I predict that the output will always be 3.
7+
// This is because the variable 'num' is defined outside the function and is always 103.
8+
// Therefore, regardless of the argument passed to getLastDigit, it will always return the last digit of 103, which is 3.
59

6-
const num = 103;
10+
// const num = 103;
711

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
12+
// function getLastDigit() {
13+
//return num.toString().slice(-1);
14+
//}
1115

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)}`);
16+
//console.log(`The last digit of 42 is ${getLastDigit(42)}`);
17+
//console.log(`The last digit of 105 is ${getLastDigit(105)}`);
18+
//console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1519

1620
// Now run the code and compare the output to your prediction
1721
// =============> write the output here
22+
// Output
23+
// The last digit of 42 is 3
24+
// The last digit of 105 is 3
25+
// The last digit of 806 is 3
26+
1827
// Explain why the output is the way it is
1928
// =============> write your explanation here
29+
// Explanation
30+
// The output will always be 3 because the function getLastDigit does not have a parameter so can not accept any arguments.
31+
2032
// Finally, correct the code to fix the problem
2133
// =============> write your new code here
34+
// New code
35+
function getLastDigit(num) {
36+
return num.toString().slice(-1);
37+
}
38+
39+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
40+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
41+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2242

2343
// This program should tell the user the last digit of each number.
2444
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)