11// Predict and explain first...
22
3+ it will print 3 for all of them
4+
35// 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+
411// =============> Write your prediction here
512
613const num = 103 ;
714
815function getLastDigit ( ) {
16+
917 return num . toString ( ) . slice ( - 1 ) ;
1018}
1119
@@ -15,9 +23,35 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1523
1624// Now run the code and compare the output to your prediction
1725// =============> 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+
1833// Explain why the output is the way it is
1934// =============> 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+
2042// 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+
2155// =============> write your new code here
2256
2357// This program should tell the user the last digit of each number.
0 commit comments