11// Predict and explain first...
2-
32// Predict the output of the following code:
43// =============> Write your prediction here
4+ //The code will always show 3 as the last digit because the function does not use the number inside getLastDigit().
55
66const num = 103 ;
77
@@ -15,10 +15,24 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515
1616// Now run the code and compare the output to your prediction
1717// =============> write the output here
18+ // The last digit of 42 is 3
19+ // The last digit of 105 is 3
20+ // The last digit of 806 is 3
21+
1822// Explain why the output is the way it is
1923// =============> write your explanation here
24+ // The function does not take any input parameter, it always uses the global variavle num = 103.
25+ // Because of this the last digit returend is always 3, regardless of the number passed in the function call.
26+
2027// Finally, correct the code to fix the problem
2128// =============> write your new code here
29+ function getLastDigit ( num ) {
30+ return num . toString ( ) . slice ( - 1 ) ;
31+ }
32+ console . log ( `The last digit of 42 is $(getlastDigita(42)}` ) ;
33+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
34+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
2235
2336// This program should tell the user the last digit of each number.
2437// Explain why getLastDigit is not working properly - correct the problem
38+ // The function was fixed by adding a parameter so it can return the last digit of the given number.
0 commit comments