11// Predict and explain first...
22
33// Predict the output of the following code:
4- // =============> Write your prediction here
4+ // This function will always return the last digit of the number 103.
5+ // This is because the variable num is hardcoded to 103 within the function scope.
56
67const num = 103 ;
78
@@ -14,11 +15,21 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1415console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
1516
1617// Now run the code and compare the output to your prediction
17- // =============> 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
19- // =============> write your explanation here
23+ // The number 103 is assigned as a constant variable so the function will always use num as 103
24+ // So when the function is called with different arguments, it still returns the last digit of 103 which is 3
25+
2026// Finally, correct the code to fix the problem
21- // =============> write your new code here
27+ function getLastDigit ( num ) {
28+ return num . toString ( ) . slice ( - 1 ) ;
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