Skip to content

Commit e6d47e5

Browse files
committed
task 2.js mandatory debug completed
1 parent 6b32611 commit e6d47e5

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

  • Sprint-3/2-mandatory-debug

‎Sprint-3/2-mandatory-debug/2.js‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,45 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// Answer below:
6+
// There is a constant variable num with value 103, the code should print last digit of the constant variable.
57

6-
const num = 103;
8+
// const num = 103;
79

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
10+
// function getLastDigit() {
11+
// return num.toString().slice(-1);
12+
// }
1113

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)}`);
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)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
// Answer below:
21+
// The last digit of 42 is 3
22+
// The last digit of 105 is 3
23+
// The last digit of 806 is 3
24+
25+
1826
// Explain why the output is the way it is
1927
// =============> write your explanation here
28+
// Answer below:
29+
// The code kept repeating same value for all log because of the constant variable decleared before the function and used inside the function.
30+
2031
// Finally, correct the code to fix the problem
2132
// =============> write your new code here
2233

34+
function getLastDigit(num) {
35+
return num.toString().slice(-1);
36+
}
37+
38+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
39+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
40+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
41+
42+
2343
// This program should tell the user the last digit of each number.
2444
// Explain why getLastDigit is not working properly - correct the problem
45+
// The global constant variable outside the function has affected the code and caused it to always reference the last number of the global variable
46+
// To fix this, the global variable will be removed from code and the function getLastDigit should take the variable name "num" assigned to the global variable.

0 commit comments

Comments
 (0)