11// Predict and explain first...
2+ /*
3+ The function 'getLastDigit' is not working properly because it does not take any parameters, yet it is being called with arguments (42, 105, 806).
4+ As a result, it always returns the last digit of the constant 'num', which is 103, instead of the last digit of the numbers passed to it.
5+ */
6+ // =============> write your prediction here
27
8+ /*
9+ The output will be:
10+ The last digit of 42 is 3
11+ The last digit of 105 is 3
12+ The last digit of 806 is 3
13+ */
314// Predict the output of the following code:
415// =============> Write your prediction here
5-
16+ /*
617const num = 103;
718
819function getLastDigit() {
@@ -12,13 +23,32 @@ function getLastDigit() {
1223console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1324console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1425console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15-
26+ */
1627// Now run the code and compare the output to your prediction
1728// =============> write the output here
29+ /*
30+ The output is:
31+ The last digit of 42 is 3
32+ The last digit of 105 is 3
33+ The last digit of 806 is 3
34+ */
1835// Explain why the output is the way it is
36+ /* 1. The function 'getLastDigit' does not accept any parameters, so it does not use the values (42, 105, 806) passed to it when called.
37+ 2. Instead, it always operates on the constant 'num', which is set to 103.
38+ 3. The function converts 'num' to a string and retrieves the last character using 'slice(-1)', which is '3'.
39+ 4. Therefore, regardless of the input values, the function always returns '3', leading to the same output for all calls.
40+ */
1941// =============> write your explanation here
2042// Finally, correct the code to fix the problem
2143// =============> write your new code here
2244
2345// This program should tell the user the last digit of each number.
2446// Explain why getLastDigit is not working properly - correct the problem
47+
48+ function getLastDigit ( num ) {
49+ return num . toString ( ) . slice ( - 1 ) ;
50+ }
51+
52+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
53+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
54+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
0 commit comments