11// Predict and explain first...
2+ // the function should get the last digit of the given parameter -
3+ // but the parameter is the already existing variable
24
35// Predict the output of the following code:
46// =============> Write your prediction here
7+ //already assigned variable num will be returned as we not defined the parameter inside the return
8+
59
610const num = 103 ;
711
8- function getLastDigit ( ) {
12+
13+ function getLastDigit ( num ) {
914 return num . toString ( ) . slice ( - 1 ) ;
1015}
1116
@@ -15,10 +20,22 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1520
1621// Now run the code and compare the output to your prediction
1722// =============> write the output here
23+ //every time we call the function we return the same number as this is already assigned inside line 10
1824// Explain why the output is the way it is
1925// =============> write your explanation here
26+ //we need to give the parameter to the function to be able to use the arguments in the console log
2027// Finally, correct the code to fix the problem
2128// =============> write your new code here
29+ //const num = 103;
30+
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)}`);
2239
2340// This program should tell the user the last digit of each number.
2441// Explain why getLastDigit is not working properly - correct the problem
0 commit comments