Skip to content

Commit e0d30d1

Browse files
committed
Solved task 2-mandatory-debug/2.js
1 parent 0b216d5 commit e0d30d1

File tree

1 file changed

+25
-6
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+25
-6
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// Predict and explain first...
22

33
// Predict the output of the following code:
4-
// =============> Write your prediction here
4+
// =============> The output will be:
5+
// 1. The last digit of 42 is 3
6+
// 2. The last digit of 105 is 3
7+
// 3. The last digit of 806 is 3
8+
// Why? Because we don't pass any parameter to the function ant it always uses the variable "num", which defined in the global scope, so function has access to it and the last digit of 103 is 3.
59

6-
const num = 103;
10+
// const num = 103;
711

812
function getLastDigit() {
913
return num.toString().slice(-1);
@@ -14,11 +18,26 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1418
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1519

1620
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
21+
// =============> The last digit of 42 is 3
22+
// The last digit of 105 is 3
23+
// The last digit of 806 is 3
1824
// Explain why the output is the way it is
19-
// =============> write your explanation here
25+
// =============> Because we use the global variable "num" in the function instead of passing the parameter to the function.
2026
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
22-
27+
//=============> const num = 103;
28+
// function getLastDigit(num) {
29+
// return num.toString().slice(-1);
30+
// }
31+
// console.log(`The last digit of 42 is ${getLastDigit(42)}`);
32+
// console.log(`The last digit of 105 is ${getLastDigit(105)}`);
33+
// console.log(`The last digit of 806 is ${getLastDigit(806)}`);
34+
//
2335
// This program should tell the user the last digit of each number.
36+
//
37+
// Now output looks correctly:
38+
// The last digit of 42 is 2
39+
// The last digit of 105 is 5
40+
// The last digit of 806 is 6
41+
//
2442
// Explain why getLastDigit is not working properly - correct the problem
43+
// Actually, maybe its not the good idea to use the same name "num" for different variables in different scopes, because it can be confusing. It works correctly but next time I'll use for the global scope some more meaningful name.

0 commit comments

Comments
 (0)