22
33// Predict the output of the following code:
44// =============> Write your prediction here
5-
6- const num = 103 ;
5+ // it will not print the correct last digits
6+ /* const num = 103;
77
88function getLastDigit() {
99 return num.toString().slice(-1);
@@ -12,13 +12,29 @@ function getLastDigit() {
1212console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1313console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15+ */
1516
1617// Now run the code and compare the output to your prediction
1718// =============> write the output here
19+ /* The last digit of 42 is 3
20+ The last digit of 105 is 3
21+ The last digit of 806 is 3
22+ */
1823// Explain why the output is the way it is
1924// =============> write your explanation here
25+
26+ // Because the method uses the num value which is 103.
2027// Finally, correct the code to fix the problem
2128// =============> write your new code here
2229
30+ function getLastDigit ( num ) {
31+ return num . toString ( ) . slice ( - 1 ) ;
32+ }
33+
34+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
35+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
36+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
37+
2338// This program should tell the user the last digit of each number.
2439// Explain why getLastDigit is not working properly - correct the problem
40+ // The function getLastDigit wasn't using intended parameters
0 commit comments