-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.js
More file actions
36 lines (29 loc) · 1.51 KB
/
Copy path2.js
File metadata and controls
36 lines (29 loc) · 1.51 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
// Predict and explain first...
// Predict the output of the following code:
// =============> Write your prediction here
//The function has no parameter, so it cannot use input values like 42, 105, 806
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
// =============> write the output here
// The out will be the last digit of 103 for all three console.log statements, because the function getLastDigit() is using the variable num which is set to 103, instead of using the input values passed to it.
// Explain why the output is the way it is
// =============> write your explanation here
//The function is not using the value passed into it.
// Finally, correct the code to fix the problem
// =============> write your new code here
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)}`);
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// If a function should work with different values → it must have parameters.
// Otherwise it will always use the same fixed value