Skip to content

Commit 038f2d7

Browse files
committed
debug 2.js
1 parent d449428 commit 038f2d7

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
// Predict and explain first...
1+
// Predict and explain first...const num is declared outside the function, so it is not accessible inside the function. This will cause a ReferenceError.
2+
// To fix this, we should pass the number as a parameter to the function and use that parameter inside the function.
23

34
// Predict the output of the following code:
4-
// =============> Write your prediction here
5+
// =============> Write your prediction here 3,3,3
56

6-
const num = 103;
7+
//const num = 103;
78

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
9+
//function getLastDigit() {
10+
//return num.toString().slice(-1);
11+
//}
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
17-
// =============> write the output here
18+
// =============> 2,5,6
1819
// Explain why the output is the way it is
19-
// =============> write your explanation here
20+
// =============> the output is the way function is called with an argument, so the function is able to access the value of the argument passed to it and return the last digit of that number.
21+
// The function is not using the variable num declared outside the function, so it does not cause a ReferenceError.
2022
// Finally, correct the code to fix the problem
2123
// =============> write your new code here
2224

2325
// This program should tell the user the last digit of each number.
2426
// Explain why getLastDigit is not working properly - correct the problem
27+
28+
const num = 103;
29+
30+
function getLastDigit(n) {
31+
return n.toString().slice(-1);
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)}`);

0 commit comments

Comments
 (0)