22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ // The "const" is hampering the function getLastDigit because the return
6+ // function will always calculate with the value of the const and ignore any
7+ // other input
8+
9+
510
611const num = 103 ;
712
@@ -15,10 +20,26 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1520
1621// Now run the code and compare the output to your prediction
1722// =============> write the output here
23+ //The last digit of 42 is 3
24+ // The last digit of 105 is 3
25+ // The last digit of 806 is 3
26+
1827// Explain why the output is the way it is
1928// =============> write your explanation here
29+ // The function getLastDigit is not working properly because it is using the variable
30+ // num, which is defined outside the function with the value of 103. It will always
31+ // return the last digit of 103 instead of the given parameter in the template literal
32+
2033// Finally, correct the code to fix the problem
2134// =============> write your new code here
2235
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+
2344// 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