33// Predict the output of the following code:
44// =============> Write your prediction here
55
6+ // The last digit of 42 is 3 undefined
7+ // The last digit of 105 is 3 undefined
8+ // The last digit of 806 is 3 undefined
9+
610const num = 103 ;
711
812function getLastDigit ( ) {
@@ -15,10 +19,31 @@ 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 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+
30+ // Because the function is not given a parameter and it is being passed an argument the same time which it ignores.
31+ // The function uses the global variable num instead and print 3
32+
2033// Finally, correct the code to fix the problem
34+
2135// =============> write your new code here
2236
37+ function getLastDigit ( num ) {
38+ return num . toString ( ) . slice ( - 1 ) ;
39+ }
40+
41+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
42+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
43+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
44+
2345// This program should tell the user the last digit of each number.
2446// Explain why getLastDigit is not working properly - correct the problem
47+
48+ // The reason was the num variable wasn't declared as a parameter inside the function.
49+ // At the same time the function is receiving arguments which the function ignores.
0 commit comments