Skip to content

Commit 51a54b4

Browse files
committed
Sprint2/2/2.js
1 parent 4e6985a commit 51a54b4

File tree

1 file changed

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

1 file changed

+22
-7
lines changed

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

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

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// PREDICTION: The function will log the last digit of the number passed to it, but it will not work as intended because the num variable is defined outside the function and is not being passed as an argument.
56

6-
const num = 103;
77

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
8+
// const num = 103;
119

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)}`);
10+
// function getLastDigit() {
11+
// return num.toString().slice(-1);
12+
// }
13+
14+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
19+
1720
// =============> write the output here
21+
// The last digit of 42 is 3
22+
// The last digit of 105 is 3
23+
// The last digit of 806 is 3
1824
// Explain why the output is the way it is
25+
// The output is not as expected because the getLastDigit function is not using the argument passed to it. Instead, it is using the num variable defined outside the function, which is not being updated with the new values.
1926
// =============> write your explanation here
27+
// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function.
2028
// Finally, correct the code to fix the problem
2129
// =============> write your new code here
30+
function getLastDigit(num) {
31+
return num.toString().slice(-1);
32+
}
2233

34+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
35+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
36+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2337
// This program should tell the user the last digit of each number.
2438
// Explain why getLastDigit is not working properly - correct the problem
39+
// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function.

0 commit comments

Comments
 (0)