22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ // The output will always be 3.This is because the function uses the variable `num`,
6+ // which is set to 103.
7+ // const num = 103;
58
6- const num = 103 ;
9+ // function getLastDigit() {
10+ // return num.toString().slice(-1);
11+ // }
712
8- function getLastDigit ( ) {
9- return num . toString ( ) . slice ( - 1 ) ;
10- }
11-
12- console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
13- console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
14- console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
13+ // console.log(`The last digit of 42 is ${getLastDigit(42)}`);
14+ // console.log(`The last digit of 105 is ${getLastDigit(105)}`);
15+ // console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516
1617// Now run the code and compare the output to your prediction
1718// =============> write the output here
19+
20+ // The last digit of 42 is 3
21+ // The last digit of 105 is 3
22+ // The last digit of 806 is 3
23+
1824// Explain why the output is the way it is
1925// =============> write your explanation here
26+
27+ // The output is always 3 because the function uses `num` (103),and ignores the number passed into it.
2028// Finally, correct the code to fix the problem
29+
2130// =============> write your new code here
2231
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 ) } ` ) ;
39+
40+
41+
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+ // We must pass the number into the function as a parameter.
0 commit comments