Credit Card Checker Help

Hello everyone! I am doing the JS Credit Card Checker project, and I am having issues with the idInvalidCardCompanies part of the project. Whenever I log the function to the console using batch as the argument, I get a log saying undefined. Here is my code:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];


// Add your functions below:

const validateCred = num => {
  let newArr = num.slice(' ');
  newArr = newArr.reverse();
  for (let i = 0; i < newArr.length; i++) {
    if (i % 2 != 0) {
      newArr[i] *= 2;
      if (newArr[i] > 9) {
        newArr[i] -= 9;
      }
    }
  }
  let total = newArr.reduce((a, b) => a + b, 0)
  if (total % 10 === 0) {
    return true;
  } else {
    return false;
  }
}


const findInvalidCards = cards => {
  let badCard = [];
  for (let j = 0; j < cards.length; j++){
    if (validateCred(cards[j]) === false) {
      badCard.push(cards[j]);
    }
  }
  return badCard;
}
console.log(findInvalidCards(batch))
const idInvalidCardCompanies = badCards => {
  let badCardCompanies = [];
  for (let k = 0; k < badCards.length; k++) {
   
    if (validateCred[k] === false) {
      if ((validateCred[k]) === 3){
        return 'Amex (American Express)';
        badCardCompanies.push(badCards[k]);
      } else if ((badCards[k]) === 4) {
        return 'Visa';
        badCardCompanies.push(badCards[k])
      } else if ((badCards[k]) === 5) {
        return 'MasterCard';
        badCardCompanies.push(badCards[k])
      } else if ((badCards[k]) === 6) {
        return 'Discover';
        badCardCompanies.push(badCards[k])
      } else {
        return 'Company not found'
      }
      
    }

  }
  
};

console.log(idInvalidCardCompanies(batch))

Any help would be appreciated!

Remember that return statements end the function when run. It seems like you want to have the if-statements push items onto the badCardCompanies array, and then return that… (if i’m understanding your intentions…)

On another note, since return statements end functions, you always want them to come last in a conditional (otherwise the code you write after will never see the light of day).

Hope this helps! :slight_smile:

if ((validateCred[k]) === 3){
        return 'Amex (American Express)';
        badCardCompanies.push(badCards[k]); // this line will never run
      }

if ((validateCred[k]) === 3){
        badCardCompanies.push(badCards[k]); // this line will run (if condition is met)!
        return 'Amex (American Express)';  
      }

What is validateCred? What does validateCred[k] return? You could add a call to console.log() to find out. Immediately before the if statement try adding console.log(validateCred[k]).

I tried switching them around, but I still am getting an answer of undefined.

That’s weird because you have an else statement that returns something (meaning if no condition is met you’d still return that (in this case a string).

I suspect then that there’s a syntax issue somewhere. Or, your for loop isn’t running to begin with (if you put a return statement outside your for loop you can test if this is the case).

The thing to do is to just break it down line by line with log statements and figure out where it’s not working for you (like @midlindner suggests).

It’s very useful to use a text editor for this kind of operation (CC is a useful platform but not the strongest text editor). I personally use sublime but I hear VS code is really good with javascript.

ValidateCred is the function that uses the Luhn Algorithm to determine if the credit card numbers are valid. so, if an element passed through doesn’t return true, the rest of the code is supposed to run.

So what is the expectation here:

What is at index k of the function? :wink:

1 Like

Change line 29-30 to this

 let newArr = num.slice(' ');
 newArr.reverse();