Skip to content

Commit 99bac7e

Browse files
Fix getLastDigit function to accept input parameter
Refactor getLastDigit function to accept a parameter and update comments for clarity.
1 parent e974ec1 commit 99bac7e

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

  • Sprint-2/2-mandatory-debug

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
// Predict and explain first...
22

3+
34
// Predict the output of the following code:
4-
// =============> Write your prediction here
5+
// =============> the function is going to return error as the /1 is not valid expression or syntax
6+
57

68
const num = 103;
79

8-
function getLastDigit() {
10+
11+
function getLastDigit(num) {
912
return num.toString().slice(-1);
1013
}
1114

15+
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+
console.log(`The last digit of 7247 is ${getLastDigit(7274)}`);
20+
1521

1622
// Now run the code and compare the output to your prediction
17-
// =============> write the output here
23+
/* ===========> function getLastDigit() {
24+
return num.toString().slice(-1);
25+
*/
1826
// Explain why the output is the way it is
19-
// =============> write your explanation here
27+
// =============> this is because the /1 is not defined and not valid in the system of javaScript languague, so it throws an error. Morever because the parameter for the function is not defined, the function takes the global variable scope to run the code inside it. So obviously, we need to put down a parameter which is num to have the function work for any number other than the global variable scope.
2028
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
29+
/* ============> function getLastDigit(num) {
30+
return num.toString().slice(-1);
31+
}
32+
*/
33+
2234

2335
// This program should tell the user the last digit of each number.
2436
// Explain why getLastDigit is not working properly - correct the problem
37+
38+
39+

0 commit comments

Comments
 (0)