Problem with nested arrays in project 'Credit Card Checker'

Hi! I’m working in this project: Credit Card Checker.

I did manage to create the function that returns true if a credit card number is valid and false if not using Luhn or mod 10 algorithm. This function takes the parameter ‘cardNum’ which is a simple array (x = [1, 2, 3…]). This function works fine with the simple arrays provided in the excersice, returning the correct value in every case. Here is the code for the function 'cardChecker (step 3):

cardChecker

But I have a problem with step n.6. I can’t figure out how to apply the function that I created to nested arrays (x = [1, 2, 3…],[1, 2, 3…],…). This is function has to analize a nested array and then add the invalid card arrays to a new variable of nested arrays. I understand that this can be done with push() and that I can access arrays using this sintax: nestedArray[indexOfArray]. Here is my attempt to create this function:

Screenshot 2020-09-24 012332

The thing is that when I call the function usign console.log I get this error:

Screenshot 2020-09-24 012409

Maybe I’m missing something, any ideas?

Hello @corewhiz75239, welcome to the forums! The error you have posted seems to correspond more with the first screenshot. The nature of the error also suggests you aren’t calling the cardCheck function properly (i.e. with a missing parameter).

1 Like

Thanks! The thing is that ‘batch’ (a nested array) variable has all the arrays that I have tested with the function ‘cardChecker’ and there was no problem. I only get the error when I call the function ‘cardChecker’ inside the function ‘invalidCards’.

Maybe what is being passed from invalidCards to cardChecker isn’t an array. In your for loop you have

numOfArray <= cardArray.length;

The length property will always be 1 more than the last index because the index is 0 based. I think what is happening is your loop is working but it gets to the last iteration of cardArray[length] (which does not exist) and passes an undefined value into cardChecker.

Hope that helps!

You are 100% right. I discovered the next day. Thanks for taking the time to look at the code.