findInvalidCards - CREDIT CARD CHECKER Project

Hello Guys,

I’m struggling and I need abit of help. On the Credit Card Checker project I have programmed the validateCred function works completely fine. However my findInvalidCards function is not working at all and instead of returning all invalid card arrays it just returns all the arrays stated in the batch variable. Can you please help me with this? I’d be most appreciative. Thank you for your time. My code is below…

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];

const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];

const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];

const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers

const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];

const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];

const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];

const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];

const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid

const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];

const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];

const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];

const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];

const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above

const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];

// Add your functions below:

const validateCred = (n) => {

//Convert number to an array of digits

let numbers = n.toString().split(’’).map(Number);

const sum = numbers /*Double every other digit from right to left or the second part */

.map((num, idx) => idx % 2 === numbers.length % 2 ? fixDouble(num * 2) : num) //Add the digits//

.reduce((acc, num) => acc += num, 0);

return sum % 10 === 0;

function fixDouble (number) {

return number > 9 ? number - 9 : number;

}
}

console.log(validateCred(4532778771091795))
/*This is the invlaid1 array inserted in the validateCred functionand it comes back as false which is correct. I’ve tested this function many times and it works fine */

const findInvalidCards = (array) => {

let invalid = ;

for (let i = 0; i < array.length; i++) {

if (validateCred(array[i]) === false) {

invalid.push(array[i]);

}

}

return invalid

}

console.log(findInvalidCards(batch)) /* returns

[ [ 4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8 ],
[ 5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9 ],
[ 3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6 ],
[ 6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5 ],
[ 4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6 ],
[ 4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5 ],
[ 5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3 ],
[ 3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4 ],
[ 6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5 ],
[ 5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4 ],
[ 3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4 ],
[ 5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9 ],
[ 6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3 ],
[ 4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3 ],
[ 4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3 ] ]

Which is all the arrays regardless whether they are invalid or valid. Please Help

1 Like

Hi, if you format your code, I’ll take a look at it :slight_smile:

Hi theophilusnelson!
I went through your code and it looks okay! The only reason it doesn’t seem to work just right is:
let numbers = n.toString().split(’’).map(Number);

Specifically, it’s the split function.
When you give your validateCred() function the number “4532778771091795” (from invalid1) , you essentially omitting the need for the split function, but when passing it the actual array, you are passing it “4,5,3,2,7,7,8,7,7,1,0,9,1,7,9,5”.
I’m sure you can see that this will not work, since you are doing split on nothing, instead of on the comma separating each number.

TL;DR
let numbers = n.toString().split(’’).map(Number);

should be:
let numbers = n.toString().split(’,’).map(Number);
Needs a comma in the argument parsed to split!

Happy coding!

Don’t worry about it. I’ve got everything sorted now. Thanks you :slightly_smiling_face:

Just a comma :laughing: Oh god coding can be a migraine at times lool. Thank you soo much. It is much apprecaited
:ok_hand: :facepunch: