22
33// Predict the output of the following code:
44// =============> Write your prediction here
5+ // The function will always return 3
56
6- const num = 103 ;
7+ // const num = 103;
78
8- function getLastDigit ( ) {
9- return num . toString ( ) . slice ( - 1 ) ;
10- }
9+ // function getLastDigit() {
10+ // return num.toString().slice(-1);
11+ // }
1112
12- console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
13- console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
14- console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
13+ // console.log(`The last digit of 42 is ${getLastDigit(42)}`);
14+ // console.log(`The last digit of 105 is ${getLastDigit(105)}`);
15+ // 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
1819// Explain why the output is the way it is
1920// =============> write your explanation here
21+ //getLastDigit takes no parameters.
22+ //Inside the function, will always return the last digit of the global variable num, which is "3".
23+
2024// Finally, correct the code to fix the problem
2125// =============> write your new code here
2226
2327// This program should tell the user the last digit of each number.
2428// Explain why getLastDigit is not working properly - correct the problem
29+ //Every call ignores the number that is passed in the argument and just returns "3"
30+ const num = 103 ;
31+
32+ function getLastDigit ( num ) {
33+ return num . toString ( ) . slice ( - 1 ) ;
34+ }
35+
36+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
37+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
38+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
0 commit comments