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
+22-7Lines changed: 22 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -2,23 +2,38 @@
2
2
3
3
// Predict the output of the following code:
4
4
// =============> Write your prediction here
5
+
// PREDICTION: The function will log the last digit of the number passed to it, but it will not work as intended because the num variable is defined outside the function and is not being passed as an argument.
5
6
6
-
constnum=103;
7
7
8
-
functiongetLastDigit(){
9
-
returnnum.toString().slice(-1);
10
-
}
8
+
// const num = 103;
11
9
12
-
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
13
-
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14
-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
10
+
// function getLastDigit() {
11
+
// return num.toString().slice(-1);
12
+
// }
13
+
14
+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15
+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16
+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
15
17
16
18
// Now run the code and compare the output to your prediction
19
+
17
20
// =============> write the output here
21
+
// The last digit of 42 is 3
22
+
// The last digit of 105 is 3
23
+
// The last digit of 806 is 3
18
24
// Explain why the output is the way it is
25
+
// The output is not as expected because the getLastDigit function is not using the argument passed to it. Instead, it is using the num variable defined outside the function, which is not being updated with the new values.
19
26
// =============> write your explanation here
27
+
// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function.
20
28
// Finally, correct the code to fix the problem
21
29
// =============> write your new code here
30
+
functiongetLastDigit(num){
31
+
returnnum.toString().slice(-1);
32
+
}
22
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)}`);
23
37
// This program should tell the user the last digit of each number.
24
38
// Explain why getLastDigit is not working properly - correct the problem
39
+
// The getLastDigit function should take a number as an argument and return the last digit of that number. However, it is currently using the num variable defined outside the function, which is not being updated with the new values passed to the function.
0 commit comments