Credit card checker - Looping through a nested array

I am supremely stuck

Track: Full Stack
Problem: I am unable to loop through a nested array and call a function on the array

Instruction: Create another function, findInvalidCards() that has one parameter for a nested array of credit card numbers. The role of findInvalidCards() is to check through the nested array for which numbers are invalid, and return another nested array of invalid cards.

My code:

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

const validateCard = arr => {
    console.log(arr);
    // reverse Array
    arr.reverse();
    console.log(arr)
    // multiply values by two 
    if(arr.length % 2 === 0) {
        arr = arr.map((e, index) => index % 2 === 1 ? e * 2 : e);
    } else {
        arr = arr.map((e,index)=> index % 2 === 0 ? e * 2 : e);
    }
    console.log(arr)
    // subtract 9 if value is greater than 9
    arr = arr.map(e => e > 9 ? e - 9 : e);
    console.log(arr)
    const arrSum = arr.reduce((acc,val) => acc + val);
    console.log(arrSum)
   

    const checkValid = arrSum % 10 === 0 ? 'valid' : 'invalid';
    console.log(checkValid);
}
validateCard(invalid5);

//Everything works perfectly except this block - Please guide me!
const findInvalidCards = arr => {
    for(let i = 0; i< arr.length; i++){
        if(bacth[i] instanceof Array){
            validateCard(arr[i]);
        } else {
            console.log(arr[i]);
        }
    }
}
console.log(findInvalidCards(bacth));

What error are you receiving, or what output are you getting that differs from what you expect?

Expected Outcome

Function findInvalidCards()` should loop through the nested array for which numbers are invalid, and return another nested array of invalid cards. It should do so by invoking validateCard function.

ValidateCard() is working but findInvalidCard is not.

Look closely at the error message. Where in your code is bacth defined? I see a similarly named array, but nothing named bacth :wink:

That was such a rookie error! You’ve been very helpful. Solved it.

1 Like

Couple of things to consider.

In your findInvalidCards function you are have a reference to batch[i] (assuming you correct the spelling).
You’ve passed the batch array to the function already, and assigned the variable arr to it, so why not use arr[i] instead? It would be more consistent, and make your function able to handle a different array if necessary.

Also, your validateCard function is currently mutating the original arrays. Print out a couple of the card numbers before and after calling your findInvalidCards function, and you’ll see that the numbers have been reversed. How could you avoid mutating the original array?

Yes, I have corrected the spelling mistake and used are[I]. Also thank you for guiding me very well, especially yesterdays comment on doing more research pushed me to do much needed reading & practice.

1 Like