22
33// Predict the output of the following code:
44// =============> Write your prediction here
5-
5+ // num is declared as a const variable of 103, this will give the same outcome because num is suppose to a placeholder not a number.
66const num = 103 ;
77
88function getLastDigit ( ) {
@@ -14,11 +14,20 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1414console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
1515
1616// Now run the code and compare the output to your prediction
17- // =============> write the output here
17+ // The output gives the same answer that is 3
18+
1819// Explain why the output is the way it is
19- // =============> write your explanation here
20+ // The variable num was declared to a const number (103) instead of a placeholder therefore it cannot be changed
2021// Finally, correct the code to fix the problem
21- // =============> write your new code here
22+ function getLastDigit ( num ) {
23+ return num . toString ( ) . slice ( - 1 ) ;
24+ }
25+
26+ console . log ( `The last digit of 42 is ${ getLastDigit ( 42 ) } ` ) ;
27+ console . log ( `The last digit of 105 is ${ getLastDigit ( 105 ) } ` ) ;
28+ console . log ( `The last digit of 806 is ${ getLastDigit ( 806 ) } ` ) ;
2229
2330// This program should tell the user the last digit of each number.
2431// Explain why getLastDigit is not working properly - correct the problem
32+ // The const variable must be removed from the code. The computer reads that first and gives the same output for all console.log statements.
33+ // A placeholder variable for num must be declared in the function parameter.
0 commit comments