Skip to content

Commit 866a84d

Browse files
committed
fix the bug in 2-mandatory-debug/2.js
1 parent 7fadd53 commit 866a84d

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

  • Sprint-2/2-mandatory-debug

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,37 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// Martin response - I predict the return value will be the last digit of num returned as a string
56

67
const num = 103;
78

89
function getLastDigit() {
910
return num.toString().slice(-1);
1011
}
1112

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)}`);
13+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
14+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
15+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1516

1617
// Now run the code and compare the output to your prediction
1718
// =============> write the output here
1819
// Explain why the output is the way it is
20+
// Martin response - the output is always 3. The function does not take a parameter and the variable declared in the global scope, num, is called repeatedly inside the function.
21+
1922
// =============> write your explanation here
23+
// Martin response - The function does not take a parameter, although the function calls include a parameter. The variable in the global scope, num, is being called repeatedly within the function and this is why all function calls return the string value of 3
24+
2025
// Finally, correct the code to fix the problem
2126
// =============> write your new code here
2227

2328
// This program should tell the user the last digit of each number.
2429
// Explain why getLastDigit is not working properly - correct the problem
30+
// Martin response - my explanation is above
31+
32+
function getLastDigit(num) {
33+
return num.toString().slice(-1);
34+
}
35+
36+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
37+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
38+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

0 commit comments

Comments
 (0)