Skip to content

Commit 9560e8d

Browse files
committed
explain and fix the code
1 parent 6a9bf82 commit 9560e8d

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

  • Sprint-2/2-mandatory-debug

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
// Predict the output of the following code:
44
// =============> Write your prediction here
5+
//============Prediction========
6+
// I think the code output will be "3" in al three cases as "num variable is declared outside the function and
7+
// not used as a parameter for the function"
58

69
const num = 103;
710

@@ -14,11 +17,30 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
1417
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
1518

1619
// Now run the code and compare the output to your prediction
20+
// prediction matched
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*/
1825
// Explain why the output is the way it is
1926
// =============> write your explanation here
27+
//===============Explanation===========
28+
// The variable is not used as a parameter of the function, and therefore
29+
// is ignored when passed inside function call.
2030
// Finally, correct the code to fix the problem
31+
//
2132
// =============> write your new code here
33+
//const num = 103;
2234

35+
function getLastDigit(num) {
36+
return num.toString().slice(-1);
37+
}
38+
39+
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
40+
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
41+
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
2342
// This program should tell the user the last digit of each number.
2443
// Explain why getLastDigit is not working properly - correct the problem
44+
// =============explanation==================
45+
// The variable is not used as a parameter of the function, and therefore
46+
// is ignored when passed inside function call.

0 commit comments

Comments
 (0)