You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sprint-2/2-mandatory-debug/2.js
+19-4Lines changed: 19 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
// Predict and explain first...
2
2
3
-
// Predict the output of the following code:
4
-
// =============> Write your prediction here
3
+
// Predict the output of the following code
4
+
// =============> Write your prediction here : The code did not set any parameter/s for the function getLastDigit. So it will always take the value of num which is 103 as a default value.
5
5
6
6
constnum=103;
7
7
@@ -14,11 +14,26 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
14
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
15
16
16
// Now run the code and compare the output to your prediction
17
-
// =============> write the output here
17
+
// =============> write the output here : The output should be as the return line (103."103".3).
18
+
/* The last digit of 42 is 3
19
+
The last digit of 105 is 3
20
+
The last digit of 806 is 3
21
+
*/
22
+
18
23
// Explain why the output is the way it is
19
-
// =============> write your explanation here
24
+
// =============> write your explanation here : Since .toString() converts the number to a string. The slice(-1) method extracts the last character from that string..
25
+
20
26
// Finally, correct the code to fix the problem
21
27
// =============> write your new code here
28
+
functiongetLastDigit(num){
29
+
returnnum.toString().slice(-1);
30
+
}
31
+
32
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
33
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
34
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
22
35
23
36
// This program should tell the user the last digit of each number.
24
37
// Explain why getLastDigit is not working properly - correct the problem
38
+
// >>>> because the pre declared value of num with const. So the function always returns the last digit of 103 which is 3.
39
+
// >>>> getLastDigit should take a parameter to work properly.
0 commit comments