Skip to content

Commit b63b502

Browse files
committed
debug exercise 2
1 parent b1699d2 commit b63b502

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

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

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> This function aims to return the number as a string using the toString method, and returns the last index in the string using the slice method with -1. the issue is that the number variable was declared outside the function as const, so the last digit will always be 3
55

66
const num = 103;
77

@@ -15,10 +15,28 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1515

1616
// Now run the code and compare the output to your prediction
1717
// =============> write the output here
18+
19+
// The last digit of 42 is 3
20+
// The last digit of 105 is 3
21+
// The last digit of 806 is 3
22+
23+
1824
// Explain why the output is the way it is
19-
// =============> write your explanation here
25+
// Because the num variable was declared outside the function which returns num with the string methods accessed with it. The function also has no parameter to receive the value passed in the function.
26+
2027
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
28+
29+
function getLastDigit(num) {
30+
return num.toString().slice(-1);
31+
}
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)}`);
37+
2238

2339
// This program should tell the user the last digit of each number.
2440
// Explain why getLastDigit is not working properly - correct the problem
41+
42+
// The reason it was not working properly is because there was no parameter that can receive the value passed in the function, and so when the function is called it defaults to the num const variable in the global scope

0 commit comments

Comments
 (0)