11// Predict and explain first...
22
33// Predict the output of the following code:
4- // =============> Write your prediction here
4+ // =============> I think the result prints will always be 3 as num will always be 103.
55
66const num = 103 ;
77
@@ -14,11 +14,23 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
1515
1616// Now run the code and compare the output to your prediction
17- // =============> write the output here
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+ // =============> because the code is using the outer variable num which is always 103.
22+ // so every call is returning the last digit of 103.
2023// Finally, correct the code to fix the problem
21- // =============> write your new code here
24+ // =============>
25+ function getLastDigit ( num ) {
26+ return num . toString ( ) . slice ( - 1 ) ;
27+ }
28+
29+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
30+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
31+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
32+
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
36+ // because there was no parameter so the function has no way of receiving the numbers. so adding a parameter fixes it.
0 commit comments