Skip to content

Commit 7c104f7

Browse files
committed
fixed getLastDigit function
1 parent edec231 commit 7c104f7

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

  • Sprint-3/2-mandatory-debug

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
// Predict the output of the following code:
44
// here we have a constant variable called num which stores the number 103 we then have declared the function getLastDigit which has an empty parameter we are then asking it to return the value stored inside num and apply the .toString method to it which turns the number 103 into a string "103" we are then taking "103" and applying the .slice method to it which will start from position -1 and return everything after it which leaves us with the string "3" then outside of the function, the function getLastDigit is being called 3 times in template literal using console.log with the arguments 42, 105 and 806 but because the variable num = "3" it will print the template literal with the "3" in the console when javascript gets to the function call for each value, which isn't the expected result.
55

6-
const num = 103;
6+
//const num = 103;
77

8-
function getLastDigit() {
9-
return num.toString().slice(-1);
10-
}
8+
//function getLastDigit() {
9+
//return num.toString().slice(-1);
10+
//}
1111

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)}`);
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)}`);
1515

1616
// Now run the code and compare the output to your prediction
1717
// output: The last digit of 42 is 3
@@ -20,7 +20,12 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2020
// Explain why the output is the way it is
2121
// the console output prints this because the the methods have only been applied to the number stored in the num variable so javascript isn't able to access the numbers passed in when the function is called in the template literal as the function getLastDigit() has no parameter so it passes in the value stored in the num variable which is "3" and so every time the function is called "3" is returned.
2222
// Finally, correct the code to fix the problem
23-
// =============> write your new code here
23+
function getLastDigit(num) {
24+
let getLastDigitResult = num.toString().slice(-1);
25+
return getLastDigitResult;
26+
}
27+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
28+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
29+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2430

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

0 commit comments

Comments
 (0)