11// Predict and explain first...
22
33// Predict the output of the following code:
4+ // Prediction: I predict that the program will print 3 for all
5+ // three numbers because the function always uses the value 103.
6+
47// =============> Write your prediction here
58
69const num = 103 ;
710
8- function getLastDigit ( ) {
9- return num . toString ( ) . slice ( - 1 ) ;
11+ function getLastDigit ( number ) {
12+ return number . toString ( ) . slice ( - 1 ) ;
1013}
1114
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 ) } ` ) ;
1518
1619// Now run the code and compare the output to your prediction
20+ // Output after correction:
21+ // The last digit of 42 is 2
22+ // The last digit of 105 is 5
23+ // The last digit of 806 is 6
24+
1725// =============> write the output here
18- // Explain why the output is the way it is
26+
27+ // Explanation: The original getLastDigit function did not have
28+ // a parameter, so the numbers 42, 105, and 806 passed to the
29+ // function were ignored. Instead, the function always used the
30+ // global value 103, so it returned 3 every time.
31+ //
32+ // The corrected function has a parameter called number.
33+ // This allows it to receive the number passed into the function.
34+
1935// =============> write your explanation here
36+
2037// Finally, correct the code to fix the problem
2138// =============> write your new code here
2239
23- // This program should tell the user the last digit of each number.
24- // Explain why getLastDigit is not working properly - correct the problem
40+ // The function now receives a number as an argument and returns
41+ // its last digit.
0 commit comments