Skip to content

Commit 2f2a720

Browse files
committed
Fix last4Digits assignment to correctly use slice on string representation of cardNumber
1 parent 27bff53 commit 2f2a720

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

  • Sprint-1/2-mandatory-errors

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
const last4Digits = cardNumber.toString().slice(-4);
33

44
// The last4Digits variable should store the last 4 digits of cardNumber
55
// However, the code isn't working
66
// Before running the code, make and explain a prediction about why the code won't work
7+
8+
The code didnt work because the slice method was called on an integer (cardNumber),
9+
but slice is a method that is only available for strings and arrays.
10+
Since cardNumber is an integer, it does not have the slice method,
11+
which will result in an error when the code is run.
12+
713
// Then run the code and see what error it gives.
14+
The error was: TypeError: cardNumber.slice is not a function
15+
816
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
17+
It gives this error because the slice method is not meant for numbers. This is what I predicted, as I expected that calling slice on an integer would result in a TypeError.
18+
919
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
20+
21+
const last4Digits = cardNumber.toString().slice(-4);

0 commit comments

Comments
 (0)