Skip to content

Commit 89a42ab

Browse files
committed
fix: convert cardNumber to string before slicing last 4 digits
1 parent f1b26c1 commit 89a42ab

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Sprint-1/errors/3.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
const numberString = cardNumber.toString();
3+
const last4Digits = numberString.slice(-4);
4+
const num = Number(last4Digits);
5+
6+
console.log(num);
37

48
// The last4Digits variable should store the last 4 digits of cardNumber
59
// However, the code isn't working
610
// Before running the code, make and explain a prediction about why the code won't work
711
// Then run the code and see what error it gives.
812
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
913
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
14+
15+
16+
17+
// explanation: to see the numbers correctly we need to convert the cardNumber to a string
18+
// first before slicing the last 4 digits. The original code was trying to use slice
19+
// a number, which is not valid. By converting it to a string first,
20+
// we can then slice the last 4 characters correctly. Then we convert it back to a number.

0 commit comments

Comments
 (0)