Skip to content

Commit 15b01b6

Browse files
committed
code fixed and explained why the previous code was not working.
1 parent 7a75478 commit 15b01b6

File tree

1 file changed

+26
-2
lines changed
  • Sprint-2/2-mandatory-debug

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
/* The output will be "The last digit of 42 is 3", "The last digit of 105 is 3", and "The last digit of 806 is 3" because
6+
the function getLastDigit always returns the last digit of the constant number 'num', which is set to 103.*/
57

8+
/*
69
const num = 103;
710
811
function getLastDigit() {
@@ -12,13 +15,34 @@ function getLastDigit() {
1215
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
1316
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1417
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
18+
*/
1519

1620
// Now run the code and compare the output to your prediction
1721
// =============> write the output here
22+
// The last digit of 42 is 3
23+
// The last digit of 105 is 3
24+
// The last digit of 806 is 3
25+
1826
// Explain why the output is the way it is
1927
// =============> write your explanation here
20-
// Finally, correct the code to fix the problem
21-
// =============> write your new code here
28+
/* The output is the way it is because the function getLastDigit does not use its parameter. Instead, it always
29+
returns the last digit of the constant variable 'num', which is set to 103. Therefore, regardless of the input
30+
value provided when calling getLastDigit, it will always return '3', the last digit of 103. To fix this, we need
31+
to modify the function to accept a parameter and return the last digit of that parameter.*/
2232

2333
// This program should tell the user the last digit of each number.
2434
// Explain why getLastDigit is not working properly - correct the problem
35+
// the return function is not using the parameter passed to it and is instead using a constant value
36+
37+
38+
// Finally, correct the code to fix the problem
39+
// =============> write your new code here
40+
41+
function getLastDigit(number) {
42+
return number.toString().slice(-1);
43+
}
44+
45+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
46+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
47+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
48+

0 commit comments

Comments
 (0)