11// Predict and explain first...
22
33// Predict the output of the following code:
4- // =============> Write your prediction here
5-
4+ // =============> the slice index can't be negative
65const num = 103 ;
76
87function getLastDigit ( ) {
@@ -14,11 +13,23 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1413console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
1514
1615// Now run the code and compare the output to your prediction
17- // =============> write the output here
16+ // =============>
17+ // The last digit of 42 is 3
18+ //The last digit of 105 is 3
19+ //The last digit of 806 is 3
1820// Explain why the output is the way it is
19- // =============> write your explanation here
21+ // =============> the constant was declared out of the function
2022// Finally, correct the code to fix the problem
21- // =============> write your new code here
23+ // =============>
24+
25+
26+ function getLastDigit ( num ) {
27+ return num . toString ( ) . slice ( - 1 ) ;
28+ }
29+
30+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
31+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
32+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
2233
2334// This program should tell the user the last digit of each number.
2435// Explain why getLastDigit is not working properly - correct the problem
0 commit comments