11// Predict and explain first...
22
3- it will print 3 for all of them
4-
53// Predict the output of the following code:
6-
7- The last digit of 42 is 3
8- The last digit of 105 is 3
9- The last digit of 806 is 3
10-
114// =============> Write your prediction here
125
136const num = 103 ;
147
158function getLastDigit ( ) {
16-
179 return num . toString ( ) . slice ( - 1 ) ;
1810}
1911
@@ -23,35 +15,9 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2315
2416// Now run the code and compare the output to your prediction
2517// =============> write the output here
26-
27- The last digit of 42 is 2
28-
29- The last digit of 105 is 5
30-
31- The last digit of 806 is 6
32-
3318// Explain why the output is the way it is
3419// =============> write your explanation here
35-
36- It always shows 3 cuz the function just uses the num at the top ( 103 ) .
37-
38- It ignores the numbers we put in ( ) like 42 , 105 , 806.
39-
40- We need to let the function take a number as input .
41-
4220// Finally, correct the code to fix the problem
43-
44- function getLastDigit ( num ) {
45-
46- return num . toString ( ) . slice ( - 1 )
47- }
48-
49- console . log ( The last digit of 42 is $ { getLastDigit ( 42 ) } )
50-
51- console . log ( The last digit of 105 is $ { getLastDigit ( 105 ) } )
52-
53- console . log ( The last digit of 806 is $ { getLastDigit ( 806 ) } )
54-
5521// =============> write your new code here
5622
5723// This program should tell the user the last digit of each number.
0 commit comments