Credit Card Checker - Need help with a little bit of code

Hey guys,

So I’m learning Javascript, and am currently doing the Credit Card Checker project.
But I’ve been stuck on the findInvalidCards() function for a day now, so I’m asking for your help.

I can’t get findInvalidCards() to return anything other than [ undefined ], and I need it to return an array of names of arrays that contain an invalid credit card number.

I have successfully made the function which determines whether or not an array contains a valid credit card number - that’s the one called validateCred right before findInvalidCards. validateCred is made to return “true” if the inputted array contains a valid number, and “false” if it doesn’t.

Here’s the GitHub:

Thanks in advance!

Couple of things.

In your iterator in findInvalidCards, i is not defined as parameter. It says i= 0 but should be let i = 0. That should fix it for now.

I have some comments though:

  • In your validateCred function you are modifying the original card array. This is not allowed. You will have to find a way to make a real copy of the card.

  • The findInvalidCards is now build to only work with the named array batch. I suggest you modify the function in such a way it takes one parameter and can be applied to any collection of cards.

2 Likes

The reason you are getting [ undefined ] is because nothing is being pushed to the array. That means that this line:

invalidCards.push(batch[i]);

is not running. Why is that? Is something stopping the if statement from getting to that line of code?

Also I believe you’re going to run into a bigger problem with scope on your usage of i in the for loops

1 Like

Thanks a lot man.

let i = 0 makes sense to me, as does adding a parameter to the findInvalidCards function.
I’m not really sure where I’m mutating the original array - maybe that’s why I’m getting arrays outputted now that doesn’t exist in the written code, haha. Guess I’ll have to find out.

Thanks again

1 Like

Thanks for the explanation!

reverse()is where you are mutating the original array. You need to use something to make a copy of the array first, then you can modify that copy how you want. MDN is very helpful

That’s what I figured - thank you :slight_smile: