Skip to content

Commit 46ff75c

Browse files
committed
Predict and solve 2.js in debug
1 parent c3dada4 commit 46ff75c

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// Answer: I believe that when we run the code we will get a Reference Error, because we are trying to
6+
// call a function getLastDigit with an argument even though the function doesn't have any parameters.
7+
// Or possibly the error might reference num in the function body being undefined.
58

9+
/*
610
const num = 103;
711
812
function getLastDigit() {
@@ -12,13 +16,46 @@ function getLastDigit() {
1216
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1317
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1418
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
19+
*/
1520

1621
// Now run the code and compare the output to your prediction
1722
// =============> write the output here
23+
// The last digit of 42 is 3
24+
// The last digit of 105 is 3
25+
// The last digit of 806 is 3
26+
1827
// Explain why the output is the way it is
1928
// =============> write your explanation here
29+
// Answer: I was wrong, there was no error message. Instead
30+
// The last digit of 42 is 3
31+
// The last digit of 105 is 3
32+
// The last digit of 806 is 3
33+
// was logged to the console. There was no Reference Error about num being undefined in the function body
34+
// because num IS defined, just above the function and has global scope, so it is reachable by the function.
35+
// Also, no error was thrown due to calling the function with arguments even though it accepted no parameters.
36+
// This is because JavaScript is a dynamic/forgiving language that rather removes surplus information and keeps
37+
// executing the code than stops it and gives an error message. So any surplus arguments passed into a function
38+
// call just gets ignored. That is why the passed arguments have no effect on the function's return value.
39+
// To make them have effect, I will add a parameter to the function, and call it num. Then when num gets accessed
40+
// in the function body, it will not be the value of the global num, but instead the function-local parameter
41+
// num.
42+
2043
// Finally, correct the code to fix the problem
2144
// =============> write your new code here
2245

46+
const num = 103;
47+
48+
function getLastDigit(num) {
49+
return num.toString().slice(-1);
50+
}
51+
52+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
53+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
54+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
55+
2356
// This program should tell the user the last digit of each number.
2457
// Explain why getLastDigit is not working properly - correct the problem
58+
// Answer: now it works as expected
59+
// The last digit of 42 is 2
60+
// The last digit of 105 is 5
61+
// The last digit of 806 is 6

0 commit comments

Comments
 (0)