This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path2.js
More file actions
38 lines (26 loc) · 1.26 KB
/
2.js
File metadata and controls
38 lines (26 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Predict and explain first...
const num = 103;
function getLastDigit() {
return num.toString().slice(-1);
}
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
// In this line, getLastDigit is called, but it doesn't use the argument 42.
// It always operates on the global constant num, which is 103.
// Therefore, it will correctly output the last digit of 103, not 42.
// To fix this issue, you should modify the getLastDigit function to accept
// a parameter and use that parameter instead of the global constant num:
// function getLastDigit(number) {
// return number.toString().slice(-1);
// }
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// Now, the function will correctly operate on the provided arguments,
// and the output will be as expected:
// The last digit of 42 is 2
// The last digit of 105 is 5
// The last digit of 806 is 6