11// Predict and explain first...
22
33// Predict the output of the following code:
4- // =============> Write your prediction here
4+ // =============>
5+ // The last digit of 42 is 3
6+ // The last digit of 105 is 3
7+ // The last digit of 806 is 3
58
69const num = 103 ;
710
@@ -14,11 +17,20 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1417console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
1518
1619// Now run the code and compare the output to your prediction
17- // =============> write the output here
20+ // =============>// The last digit of 42 is 3
21+ // The last digit of 105 is 3
22+ // The last digit of 806 is 3
1823// Explain why the output is the way it is
19- // =============> write your explanation here
24+ // =============> the variable num is assigned to the value 103 in the global scope and not assigned to any value in the function scope
2025// Finally, correct the code to fix the problem
2126// =============> write your new code here
27+ function getLastDigit ( num ) { // we add the identier num as parameter to the function getLastDigit
28+ return num . toString ( ) . slice ( - 1 ) ;
29+ }
30+
31+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
32+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
33+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
2234
2335// This program should tell the user the last digit of each number.
2436// Explain why getLastDigit is not working properly - correct the problem
0 commit comments