Skip to content

Commit 8176900

Browse files
refactored and fixed the error so that the cardNumber is now correctly converted into a string so that .Slice can be used and produce no errors.
1 parent 7ae8d3a commit 8176900

File tree

1 file changed

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

1 file changed

+12
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
// const cardNumber = 4533787178994213;
2+
// const last4Digits = cardNumber.slice(-4);
3+
// prediction: The code will not work because the `slice` method is being called on a number and has not yet been converted into a string.
4+
5+
// this code will throw an error.
6+
// console.log(last4Digits);
7+
// The error i was given: cardNumber.slice is not a function
8+
// Explanation: The error occurs because cardNumber is a number, and the slice method is for a string method.
9+
10+
// To fix this, we need to convert `cardNumber` to a string before using the `slice` method by adding .toString() to the cardNumber variable.
111
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
12+
const last4Digits = cardNumber.toString().slice(-4);
13+
console.log(last4Digits); // This should now correctly log "4213"
314

415
// The last4Digits variable should store the last 4 digits of cardNumber
516
// However, the code isn't working

0 commit comments

Comments
 (0)