22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ // the result will be 3 times
6+ // const num = 103;
57
6- const num = 103 ;
8+ // function getLastDigit() {
9+ // return num.toString().slice(-1);
10+ // }
711
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 ) } ` ) ;
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
1821// Explain why the output is the way it is
1922// =============> write your explanation here
23+ // because num is defined as 103 and not taking any input from the function parameter.
2024// Finally, correct the code to fix the problem
2125// =============> write your new code here
2226
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 ) } ` ) ;
2334// This program should tell the user the last digit of each number.
2435// 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.
0 commit comments