This repository was archived by the owner on Apr 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.js
More file actions
35 lines (27 loc) · 1.17 KB
/
2.js
File metadata and controls
35 lines (27 loc) · 1.17 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
// Predict and explain first...
// The function intends to take a number and return it's last digit
// Each call of the function will return the same result
// Predict the output of the following code:
// 'The last digit of 42 is 3'
// 'The last digit of 105 is 3'
// 'The last digit of 806 is 3'
// const num = 103;
// function getLastDigit() {
// return num.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)}`);
// Now run the code and compare the output to your prediction
// 'The last digit of 42 is 3'
// 'The last digit of 105 is 3'
// 'The last digit of 806 is 3'
// Explain why the output is the way it is
// Because the function was not correctly defined to accept an argument, It was using a fixed variable instead
// Finally, correct the code to fix the problem
function getLastDigit(num) {
return num.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)}`);