22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ /* The output will be "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3" because
6+ the function getLastDigit always returns the last digit of the constant number 'num', which is set to 103.*/
57
8+ /*
69const num = 103;
710
811function getLastDigit() {
@@ -12,13 +15,34 @@ function getLastDigit() {
1215console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1316console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1417console.log(`The last digit of 806 is ${getLastDigit(806)}`);
18+ */
1519
1620// Now run the code and compare the output to your prediction
1721// =============> write the output here
22+ // The last digit of 42 is 3
23+ // The last digit of 105 is 3
24+ // The last digit of 806 is 3
25+
1826// Explain why the output is the way it is
1927// =============> write your explanation here
20- // Finally, correct the code to fix the problem
21- // =============> write your new code here
28+ /* The output is the way it is because the function getLastDigit does not use its parameter. Instead, it always
29+ returns the last digit of the constant variable 'num', which is set to 103. Therefore, regardless of the input
30+ value provided when calling getLastDigit, it will always return '3', the last digit of 103. To fix this, we need
31+ to modify the function to accept a parameter and return the last digit of that parameter.*/
2232
2333// This program should tell the user the last digit of each number.
2434// Explain why getLastDigit is not working properly - correct the problem
35+ // the return function is not using the parameter passed to it and is instead using a constant value
36+
37+
38+ // Finally, correct the code to fix the problem
39+ // =============> write your new code here
40+
41+ function getLastDigit ( number ) {
42+ return number . toString ( ) . slice ( - 1 ) ;
43+ }
44+
45+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
46+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
47+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
48+
0 commit comments