Skip to content

Commit 6123bb4

Browse files
committed
3.js
1 parent eff1a8f commit 6123bb4

File tree

1 file changed

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

1 file changed

+20
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1-
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
1+
// const cardNumber = 4533787178994213;
2+
// const last4Digits = cardNumber.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
77
// Then run the code and see what error it gives.
88
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
99
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
10+
11+
12+
13+
/* This code throws an error because `cardNumber` is a **number**, and numbers do not have a `.slice()` method—`.slice()` is a method for strings and arrays.
14+
15+
**How to fix:**
16+
Convert `cardNumber` to a string before using `.slice()`:
17+
18+
````javascript
19+
const cardNumber = 4533787178994213;
20+
const last4Digits = cardNumber.toString().slice(-4);
21+
````
22+
23+
Now, `last4Digits` will correctly contain the last 4 digits as a string.*/
24+
25+
const cardNumber = 4533787178994213;
26+
const last4Digits = cardNumber.toString().slice(-4);
27+
console.log(last4Digits);

0 commit comments

Comments
 (0)