Skip to content

Commit 901a010

Browse files
Committing the actions for 2.js
1 parent 5f4ae71 commit 901a010

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

  • Sprint-2/2-mandatory-debug

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,41 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
// I predict that the three console.log messages will always output 3.
6+
// This is due to the fact that the const num is defined as 103 and the program will always use that global value as the source of truth.
57

68
const num = 103;
79

810
function getLastDigit() {
911
return num.toString().slice(-1);
1012
}
1113

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)}`);
14+
//console.log(`The last digit of 42 is ${getLastDigit(42)}`);
15+
//console.log(`The last digit of 105 is ${getLastDigit(105)}`);
16+
//console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1517

1618
// Now run the code and compare the output to your prediction
1719
// =============> write the output here
20+
// The last digit of 42 is 3
21+
// The last digit of 105 is 3
22+
// The last digit of 806 is 3
23+
1824
// Explain why the output is the way it is
1925
// =============> write your explanation here
26+
// The global value that is defined outside of the function is used as the source of truth.
27+
// Therefore when the program executes the line return num.toString().slice(-1); it always considers the global value.
28+
// The console.log ignores the values.
29+
2030
// Finally, correct the code to fix the problem
2131
// =============> write your new code here
2232

33+
function getLastDigit(num) {
34+
return num.toString().slice(-1);
35+
}
36+
37+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
38+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
39+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
40+
2341
// This program should tell the user the last digit of each number.
2442
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)