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
{{ message }}
Repository navigation
Commit b63b502
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: Sprint-3/2-mandatory-debug/2.js
+21-3Lines changed: 21 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
// Predict and explain first...
2
2
3
3
// Predict the output of the following code:
4
-
// =============> Write your prediction here
4
+
// =============> This function aims to return the number as a string using the toString method, and returns the last index in the string using the slice method with -1. the issue is that the number variable was declared outside the function as const, so the last digit will always be 3
5
5
6
6
constnum=103;
7
7
@@ -15,10 +15,28 @@ 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
17
// =============> write the output here
18
+
19
+
// The last digit of 42 is 3
20
+
// The last digit of 105 is 3
21
+
// The last digit of 806 is 3
22
+
23
+
18
24
// Explain why the output is the way it is
19
-
// =============> write your explanation here
25
+
// Because the num variable was declared outside the function which returns num with the string methods accessed with it. The function also has no parameter to receive the value passed in the function.
26
+
20
27
// Finally, correct the code to fix the problem
21
-
// =============> write your new code here
28
+
29
+
functiongetLastDigit(num){
30
+
returnnum.toString().slice(-1);
31
+
}
32
+
33
+
34
+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
35
+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
36
+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
37
+
22
38
23
39
// This program should tell the user the last digit of each number.
24
40
// Explain why getLastDigit is not working properly - correct the problem
41
+
42
+
// The reason it was not working properly is because there was no parameter that can receive the value passed in the function, and so when the function is called it defaults to the num const variable in the global scope
0 commit comments