Skip to content

Commit e4e3d07

Browse files
committed
Answer to 2-mandatory-errors/3.js code updated and explanation included as text
1 parent ad67e7b commit e4e3d07

File tree

1 file changed

+8
-1
lines changed
  • Sprint-1/2-mandatory-errors

1 file changed

+8
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
//const last4Digits = cardNumber.slice(-4);
3+
4+
5+
// convert number → string, then slice the last 4 chars
6+
const last4 = cardNumber.toString().slice(-4);
7+
8+
console.log(last4); // "4213"
39

410
// The last4Digits variable should store the last 4 digits of cardNumber
511
// However, the code isn't working
612
// Before running the code, make and explain a prediction about why the code won't work
713
// Then run the code and see what error it gives.
814
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
915
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
16+
// cardNumber is a variable that holds a number, and numbers do not have a slice method. The slice method is used for strings and arrays, but not for numbers. To fix this error, we need to convert the cardNumber to a string before using the slice method. We can do this by using the toString() method on cardNumber, like this: cardNumber.toString().slice(-4). This will convert the number to a string and then allow us to use the slice method to get the last 4 digits.

0 commit comments

Comments
 (0)