22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ //The output will be:
6+ // The last digit of 42 is 3
7+ // The last digit of 105 is 3
8+ // The last digit of 806 is 3
9+ //
10+ // All outputs show "3" because the function not taking the parameter
11+ // passe value to 'num' (which is 103), so it returns the last digit of 103, which is 3.
512
613const num = 103 ;
714
@@ -15,10 +22,34 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1522
1623// Now run the code and compare the output to your prediction
1724// =============> write the output here
25+
26+ // The last digit of 42 is 3
27+ // The last digit of 105 is 3
28+ // The last digit of 806 is 3
1829// Explain why the output is the way it is
1930// =============> write your explanation here
31+ // The function getLastDigit() has TWO problems:
32+ // The function definition doesn't have a parameter,
33+ // it can't receive the values (42, 105, 806) added to it.
34+ //inside the function, it uses the global constant
35+ // 'num' (which is 103) instead of using the parameter
36+ //getLastDigit(42) call function not taking 42
37+ // num (103) converts to "103"
38+ // -slice(-1) gets the last character "3"
39+ // returns "3"
2040// Finally, correct the code to fix the problem
2141// =============> write your new code here
2242
43+ const num1 = 103 ;
44+ function getLastDigit ( number ) { // Added parameter 'number'
45+ return number . toString ( ) . slice ( - 1 ) ; // Use 'number' instead of 'num'
46+ }
47+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
48+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
49+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
50+
2351// This program should tell the user the last digit of each number.
2452// Explain why getLastDigit is not working properly - correct the problem
53+ //The function has no parameter when call getLastDigit(42),
54+ // the number 42 is passed but storage variable to save it.
55+ // The function can't use it because it doesn't have a parameter to receive it.
0 commit comments