22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ //============Prediction========
6+ // I think the code output will be "3" in al three cases as "num variable is declared outside the function and
7+ // not used as a parameter for the function"
58
69const num = 103 ;
710
@@ -14,11 +17,30 @@ 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
20+ // prediction matched
1721// =============> write the output here
22+ /*The last digit of 42 is 3
23+ The last digit of 105 is 3
24+ The last digit of 806 is 3*/
1825// Explain why the output is the way it is
1926// =============> write your explanation here
27+ //===============Explanation===========
28+ // The variable is not used as a parameter of the function, and therefore
29+ // is ignored when passed inside function call.
2030// Finally, correct the code to fix the problem
31+ //
2132// =============> write your new code here
33+ //const num = 103;
2234
35+ function getLastDigit ( num ) {
36+ return num . toString ( ) . slice ( - 1 ) ;
37+ }
38+
39+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
40+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
41+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
2342// This program should tell the user the last digit of each number.
2443// Explain why getLastDigit is not working properly - correct the problem
44+ // =============explanation==================
45+ // The variable is not used as a parameter of the function, and therefore
46+ // is ignored when passed inside function call.
0 commit comments