11// Predict and explain first...
22
33// Predict the output of the following code:
4- // =============> Write your prediction here
4+ // =============> the function has no parameter so will use the
5+ // global value of num for num.toString which means it will always
6+ // return 3 which is the last digit of 103
7+
58
69const num = 103 ;
710
@@ -14,11 +17,38 @@ console.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
17- // =============> write the output here
20+ // =============> The last digit of 42 is 3
21+ //The last digit of 105 is 3
22+ //The last digit of 806 is 3
23+
24+
1825// Explain why the output is the way it is
19- // =============> write your explanation here
26+ // =============> the function has no parameter so will use the
27+ // global value of num for num.toString
28+
2029// Finally, correct the code to fix the problem
21- // =============> write your new code here
30+ // =============>
31+ // function getLastDigit(num) {
32+ // return num.toString().slice(-1);
33+ // }
34+
35+ // console.log(`The last digit of 42 is ${getLastDigit(42)}`);
36+ // console.log(`The last digit of 105 is ${getLastDigit(105)}`);
37+ // console.log(`The last digit of 806 is ${getLastDigit(806)}`);
38+
39+ // removed const num = 103; and then added num as a parameter to the function
40+ // getLastDigit so that it can take in the value of num when the function is
41+ // called and return the last digit of that number instead of always using
42+ // the global value of num which was 103.
2243
2344// This program should tell the user the last digit of each number.
2445// Explain why getLastDigit is not working properly - correct the problem
46+
47+ // num was not declared within the function so the global value of num in the
48+ // the const declaraton was used for each number
49+ //I corrected this by putting num as a parmaeter for the function so that it can take
50+ //in the value of num when the function is run for any number and return the last digit
51+ //of the number instead of always using the fixed value of num which was 103.
52+ //I also removed the const declaration of num because it was not needed and was causing
53+ //confusion as to which value of num was being used in the function.
54+
0 commit comments