33// Predict the output of the following code:
44// =============> Write your prediction here
55
6+ // I predict that the code will output 'undefined' for each call to getLastDigit because the function does not use its parameter.
7+
8+ // This program should tell the user the last digit of each number.
9+
610const num = 103 ;
711
812function getLastDigit ( ) {
@@ -15,10 +19,27 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1519
1620// Now run the code and compare the output to your prediction
1721// =============> write the output here
22+
23+ // The output will be:
24+ // The last digit of 42 is undefined
25+ // The last digit of 105 is undefined
26+ // The last digit of 806 is undefined
27+
1828// Explain why the output is the way it is
1929// =============> write your explanation here
30+ // The output is 'undefined' because the function getLastDigit does not use its parameter.
31+ // It always returns the last digit of the variable num, which is 103, not the number passed to it.
32+
2033// Finally, correct the code to fix the problem
2134// =============> write your new code here
2235
23- // This program should tell the user the last digit of each number.
36+ function getLastDigit ( num ) {
37+ return num . toString ( ) . slice ( - 1 ) ;
38+ }
39+
40+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
41+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
42+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
43+
44+ // This program should tell the user the last digit of each number.
2445// Explain why getLastDigit is not working properly - correct the problem
0 commit comments