22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ // Since getLastDigit() does not declare any parameters, the arguments passed when calling it are going to be ignored
56
67const num = 103 ;
78
@@ -15,10 +16,25 @@ 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+ // The last digit of 42 is 3
20+ // The last digit of 105 is 3
21+ // The last digit of 806 is 3
1822// Explain why the output is the way it is
1923// =============> write your explanation here
24+ // The function getLastDigit() does not any parameters, but it is being called with argument:
25+ // getLastDigit(42), getLastDigit(105), and getLastDigit(806). Inside the function, it always uses the global const num, which is set to 103.
26+ // That means every call ignores the arguments and always returns the last digit of 103, which is "3"
2027// Finally, correct the code to fix the problem
2128// =============> write your new code here
29+ function getLastDigit ( num ) {
30+ return num . toString ( ) . slice ( - 1 ) ;
31+
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+
2238
2339// This program should tell the user the last digit of each number.
2440// Explain why getLastDigit is not working properly - correct the problem
0 commit comments