22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ //The output will be 3 in each expression.
56
67const num = 103 ;
78
@@ -15,10 +16,28 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516
1617// Now run the code and compare the output to your prediction
1718// =============> write the output here
19+ // The output is:
20+ // The last digit of 42 is 3
21+ // The last digit of 105 is 3
22+ // The last digit of 806 is 3
23+
1824// Explain why the output is the way it is
1925// =============> write your explanation here
26+ // The function `getLastDigit` is not taking any arguments, so it always returns the last digit which is 103.
27+
2028// Finally, correct the code to fix the problem
2129// =============> write your new code here
30+ function getLastDigit ( num ) {
31+ return num . toString ( ) . slice ( - 1 ) ;
32+ }
33+
34+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
35+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
36+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
2237
2338// This program should tell the user the last digit of each number.
2439// Explain why getLastDigit is not working properly - correct the problem
40+
41+ // The function `getLastDigit` is not working properly because it was using `num`,
42+ // instead of the parameter passed to the function. By changing the function to accept a parameter `num`,
43+ // it now correctly returns the last digit of the number passed to it..
0 commit comments