Skip to content

Commit 709bed6

Browse files
authored
Predict, consider the error and update the code.
1 parent 0e312da commit 709bed6

File tree

1 file changed

+15
-0
lines changed
  • Sprint-1/2-mandatory-errors

1 file changed

+15
-0
lines changed

Sprint-1/2-mandatory-errors/3.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
/* The old code:
12
const cardNumber = 4533787178994213;
23
const last4Digits = cardNumber.slice(-4);
4+
*/
35

46
// The last4Digits variable should store the last 4 digits of cardNumber
57
// However, the code isn't working
68
// Before running the code, make and explain a prediction about why the code won't work
79
// Then run the code and see what error it gives.
810
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
911
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
12+
13+
/* My prediction: The code will give an error because the minus sign(-4) and slice works only with strings, not numbers.
14+
15+
Consideration: The error is a TypeError: cardNumber.slice is not a function. mostly it was because I predicted that numbers do not have a slice method. About the minus sign, I have learned about positive and negative indexing in strings, now I can see the difference.
16+
17+
18+
To fix this, we need to convert cardNumber to a string before calling the slice method.
19+
*/
20+
21+
// Updated code:
22+
const cardNumber = 4533787178994213;
23+
const last4DigitsCorrected = cardNumber.toString().slice(-4);
24+
console.log(last4DigitsCorrected);

0 commit comments

Comments
 (0)