Skip to content

Commit 12f14c1

Browse files
committed
complete ex 2.js
1 parent af75a19 commit 12f14c1

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> I predict the result will be wrong because the num is set set on 103.
55

6-
const num = 103;
6+
/*const num = 103;
77
88
function getLastDigit() {
99
return num.toString().slice(-1);
1010
}
1111
1212
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1313
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 806 is ${getLastDigit(806)}`);*/
1515

1616
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
17+
/* =============> the output:
18+
The last digit of 42 is 3
19+
The last digit of 105 is 3
20+
The last digit of 806 is 3*/
1821
// Explain why the output is the way it is
19-
// =============> write your explanation here
22+
/* =============> The problem is that the function ignores the numbers passed to it,
23+
it always uses num, which is 103, so it returns 3 every time.*/
2024
// Finally, correct the code to fix the problem
2125
// =============> write your new code here
26+
/*function getLastDigit() {
27+
return num.toString().slice(-1);
28+
}
2229
30+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
31+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
32+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);*/
2333
// This program should tell the user the last digit of each number.
2434
// Explain why getLastDigit is not working properly - correct the problem
35+
// In this case getLastDigit is not working because it's parameter is empty and not declared.
36+
// Here's the right code:
37+
function getLastDigit(num) {
38+
return num.toString().slice(-1);
39+
}
40+
41+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
42+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
43+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

0 commit comments

Comments
 (0)