11// Predict and explain first...
22
33// Predict the output of the following code:
4- // =============> Write your prediction here
4+ // I think the output will be wrong because the function does not use
5+ // the numbers passed into it. It always uses the const num = 103,
6+ // so it will always return 3.
57
68const num = 103 ;
79
@@ -14,11 +16,31 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1416console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
1517
1618// Now run the code and compare the output to your prediction
17- // =============> write the output here
19+ // The last digit of 42 is 3
20+ // The last digit of 105 is 3
21+ // The last digit of 806 is 3
22+
1823// Explain why the output is the way it is
19- // =============> write your explanation here
24+
25+ // The function is not working properly because it does not have a
26+ // parameter to receive the numbers. It uses the global variable num,
27+ // which is always 103, so it always returns the last digit of 103.
28+
29+
2030// Finally, correct the code to fix the problem
21- // =============> write your new code here
31+
32+ function getLastDigit ( num ) {
33+ return num . toString ( ) . slice ( - 1 ) ;
34+ }
35+
36+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
37+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
38+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
2239
2340// This program should tell the user the last digit of each number.
2441// Explain why getLastDigit is not working properly - correct the problem
42+
43+ // The function is not working properly because it does not use the
44+ // number that is passed into it. It uses the global variable `num`,
45+ // which is always 103, so it always returns 3.
46+ // The function needs a parameter so it can use each number given to it.
0 commit comments