Credit Card Checker stuck in finding invalid cards

So, I’ve been going through my code several times now and can’t figure out why it doesn’t work. I’ve even changed it to follow the same logic as some of the working examples I were able to find here on other forum topics.

Validate function works perfectly, no issue what so ever.

const findInvalidCards = (cards) => {
    let invalidCards = [];

    for (card of cards) {
        if (!validateCred(card)) {
            invalidCards.push(card);
        }
    }
    console.log(invalidCards);
    return invalidCards;
}

One thing I noticed is that the ‘card’ variable checks out as unresolved, but this exact code seemed to work for other people. What gives?

Are you getting an error message, or is the code just not returning what it should be? If you are getting an error message what is it, and if not could you post your full code. Sometimes each individual element works completely fine in isolation but breaks when trying to use them together!

1 Like

Not getting an error, but it doesn’t seem to return anything - that, or my logging is faulty.
I am trying to log the following at the end of the file (outside the scope):

console.log(validateCred(valid1)); // returns true

If I try to log the findInvalidCards function with ‘cards’ as a parameter, it only logs the numbers within the batch array - like it’s not going deep enough in the nested array or something?

What I would say is try console.log at various steps, inside the loop, outside the loop, outside the function etc, to see if you can find where it’s stopping. I don’t see anything off the top of my head wrong with the code so you’ll have to do some debugging using the console.

EDIT

As an update to this, I copied your code into my finished correct code and used it as my `findInvalidCards` function, and it returned exactly the same results. So my suspicion now is that there is actually some issue with the `validateCred` function. Maybe try it out with a regular `for` loop instead of a `for...of` just to see if that changes things, and also check your `validateCred` function as while it works individually, it might be doing something incompatible with your `if` statement.

@adamgaffney137183916 is right about the log statements.

I highly suspect the trouble is around the if condition parameter since the rest of the code seems like it does what it’s supposed to be doing.

1 Like

Where in your code is cards defined? What is it assigned to? (The argument to your findInvalidCards function) What output are you getting? Seeing your entire code would eliminate a lot of guessing on our part.

I have defined cards by changing the name of the batch function that was initially included with the project file.

Here is the rest of my code:

const validateCred = (card) => {
    let cardInv = card.slice().reverse();

    for (let i = 0; i < cardInv.length; i++) {
        if (i % 2 != 0) {
            cardInv[i] = cardInv[i] * 2;
        }

        if (cardInv[i] > 9) {
            cardInv[i] -= 9;
        }
    }

    let sum = cardInv.reduce((a, b) => a + b, 0);

    return sum % 10 === 0;
}

const findInvalidCards = (cards) => {
    let invalidCards = [];

    for (card of cards) {
        if (!validateCred(card)) {
            invalidCards.push(card);
        }
    }
    console.log(invalidCards);
    return invalidCards;
}

// Debug
console.log(findInvalidCards);
console.log(validateCred(valid1));

What is the output of this? You’re logging the function itself rather than calling the function with an argument, and logging the return value.

Ah, that’s because I didn’t figure out how to get it’s output to anything that makes sense. Right now it’s logging [Function: findInvalidCards] as expected.

I don’t have a clue on what I should be logging there. I tried findInvalidCards(cards) but that seems to output the arrays included in the batch function (which is renamed to cards, in my case).

You should be getting an array of arrays. The nested arrays should be the invalid cards from the original batch array which you’ve renamed cards. Is that not what you get when you execute console.log(findInvalidCards(cards))?

1 Like

It is - but isn’t it supposed to… I don’t know, process those invalid arrays in some way? Forgive me if I misunderstood something from the instructions - I thought my code was faulty and didn’t want to proceed any further. :sweat_smile:

As long as the only card numbers in the returned array are the invalid cards, you’re right on track.

I am not sure if this is correct but I run this code. I was seeing other codes about the function findInvalidCards and I tried to run it, but it did not work for me. So I tested with this code.
To test it I used the array invalid2;

Please advise me. Thank you so much in advance.

function findInvalidCards(cards){
    let invalidCard = [];
    for (let i = 0; i < cards.length; i++) {
      if(!validateCred(cards)) {
        invalidCard.push(cards[i]);
      }
    }
    return invalidCard;
  }
  console.log(findInvalidCards(invalid2));