Credit Card Checker 5: looping through nested arrays

Link to problem here
Ok, I’ve tried a bunch of variations for step 5 in the credit card checker, but I have no idea how to get this function to work. I try looping through, but I don’t get how to loop through the first index and keep the second constant at 0 in a nested array. I’ve tried doing a nested loop, but that doesn’t make sense to me considering it’s supposed to stay at 0. I always get “TypeError: Cannot read property ‘0’ of undefined.”
invalidCards is the array I made that holds all invalid card numbers inside of their own array: [[invalid card number], etc.]

let invalidCardSuppliers = [];

const idInvalidCardCompanies = cardArray => {
    for (let index = 0; index <= cardArray.length; index++) {
        if (cardArray[index][0] === 3) {
            invalidCardSuppliers.push('Amex (American Express)');
        } else if (cardArray[index][0] === 4) {
            invalidCardSuppliers.push('Visa');
        } else if (cardArray[index][0] === 5) {
            invalidCardSuppliers.push('Mastercard');
        } else if (cardArray[index][0] === 6) {
            invalidCardSuppliers.push('Discovery');
        } else {
            invalidCardSuppliers.push('Company not found');
        }
    }
    return invalidCardSuppliers;      
};

console.log(idInvalidCardCompanies(invalidCards));

and here is my attempt with a switch:

const idInvalidCardCompanies = cardArray => {
    for (let index = 0; index <= cardArray.length; index++) {
        switch (cardArray[index][0]) {
            case 3 :
                invalidCardSuppliers.push('Amex (American Express)');
                break;
            case 4 :
                invalidCardSuppliers.push('Visa');
                break;
            case 5 :
                invalidCardSuppliers.push('Mastercard');
                break;
            case 6 :
                invalidCardSuppliers.push('Discovery');
                break;
            default :
                invalidCardSuppliers.push('Company not found');
                break;
        }
    }
    return invalidCardSuppliers;
};
console.log(invalidCardCompanies(invalidCards);

If you read the error message, it’s not the 0 that is the problem, you already didn’t have a value at all, you dropped the ball at an earlier point. Where?

There’s some repetition going on here. There’s nothing different happening for the different cards, they don’t need their own code.

You can do this:

const cards = [
  [3, 'Amex (American Express)'],
  [4, 'Visa'],
  [5, 'Mastercard'],
  [6, 'Discovery']
]

And then a loop to check each one. Furthermore, it doesn’t need to repeatedly need to look up the first value. Look up the first value, then see if it’s among the known cards. Even better yet, you can use an object to map numbers to companies, then you won’t even need to search, you can look it up.

You also only need to consider ONE card at a time. You can then repeat that action for each card.
The action for a card, and the repetition of the action, those are two separate things:

cards.map(classifyCard)

Ok, I hadn’t gotten to that lesson yet so I went over .map() and tried it again, however, I don’t think I really understand how it works. When I would call a specific index, it returns the card names in the right order. However, when I call the function, it returns them in the order that they’re put in inside of the cards array, not in the order they occur.

const idInvalidCardCompanies = cards.map(classifyCard => {
    //return classifyCard; // returns cards
    for(let index = 0; index < cards.length; index++) {
        for (let index2 = 0; index2 < invalidCards.length; index2++) {
            if (classifyCard[index] === invalidCards[index2][0]) {
                invalidCardSuppliers.push(classifyCard[1]);
            }
        }
    }
    for (let index = 0; index < invalidCardSuppliers.length; index++) {
        for (let index2 = 0; index2 < invalidCardSuppliers.length; index++) {
            if (invalidCardSuppliers[index] === invalidCardSuppliers[index2]) {
                return invalidCardSuppliers.pop(invalidCardSuppliers[index2]);
                }
            }
        }
    return invalidCardSuppliers;
});
console.log(idInvalidCardCompanies) 
//output: ['Amex (American Express)', 'Visa', 'Mastercard', 'Discovery']
  1. instructions say to start from the back, you’re going forwards
  2. make a new function that only takes 1 card as input, that way you have 1 less level of nesting to deal with/look at

Do it with pen and paper first, no point writing code until you’ve figured out what it’s supposed to be doing.