Skip to content

Commit dcfce87

Browse files
add num as a parameter to function
1 parent c2177db commit dcfce87

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
// Predict and explain first...
2+
// In my idea we can not change value of num as it is defined by const
23

34
// Predict the output of the following code:
45
// =============> Write your prediction here
6+
//perhaps we will get value definition error
57

6-
const num = 103;
8+
/*const num = 103;
79
810
function getLastDigit() {
911
return num.toString().slice(-1);
1012
}
1113
1214
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1315
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
14-
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
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
19-
// =============> write your explanation here
25+
// I was wrong in all the function call, the result is the same because the function always uses the global num.
26+
2027
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
28+
let num = 103;
29+
30+
function getLastDigit(num) {
31+
return num.toString().slice(-1);
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)}`);
2237

2338
// This program should tell the user the last digit of each number.
2439
// Explain why getLastDigit is not working properly - correct the problem

0 commit comments

Comments
 (0)