22
33// Predict the output of the following code:
44// =============> Write your prediction here
5- // the result will be 3
6- // const num = 103;
75
8- // function getLastDigit() {
9- // return num.toString().slice(-1);
10- // }
6+ const num = 103 ;
117
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)}`);
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 ) } ` ) ;
1515
1616// Now run the code and compare the output to your prediction
1717// =============> write the output here
18- //The last digit of 42 is 3
19- // The last digit of 105 is 3
20- // The last digit of 806 is 3
2118// Explain why the output is the way it is
2219// =============> write your explanation here
23- // because num is defined as 103 and not taking any input from the function parameter.
2420// Finally, correct the code to fix the problem
2521// =============> write your new code here
2622
27- function getLastDigit ( num ) {
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 ) } ` ) ;
3423// This program should tell the user the last digit of each number.
35- // Explain why getLastDigit is not working properly - correct the problem
36- // because num is defined as 103 and not taking any input from the function parameter.
24+ // Explain why getLastDigit is not working properly - correct the problem
0 commit comments