Challenge Project: Credit Card Checker

Hello, I’ve searched everywhere I can think of and I cannot find someone to explain or help me in my situation. It’s probably very simple but I just cannot figure it out.

When I run the following code:

const validateCred = (arr) => {
  const reversedArray = arr.reverse();
  let singleNumbers = [];
  let doubleNumbers = [];
  for (let i = 0; i < reversedArray.length; i = i + 2) {
    if (i === 0) {
      i++;
    }
    let doubleNum = reversedArray[i] * 2;
    if (doubleNum > 9) {
      doubleNum -= 9;
    };
    doubleNumbers.push(doubleNum);
  };
  for (let index = 0; index < reversedArray.length; index = index + 2) {
    singleNumbers.push(reversedArray[index]);
  };
  let newArray = singleNumbers.concat(doubleNumbers);
  let sumOfCard = newArray.reduce((element1, element2) => element1 + element2);
  sumOfCard = sumOfCard % 10;
  if (sumOfCard === 0) {
    return 'This card is valid.';
  } else {
    return 'This card is invalid.';
  };
};

const findInvalidCards = (arr) => {
  let invalidCards = [];
  let cardResults = [];
  for (let x = 0; x < arr.length; x++) {
    cardResults = arr[x].map(validateCred());
  };
};
 

console.log(findInvalidCards(batch));

I expect to receive an array of strings. Either ’ This card is valid.’ or ‘This card is invalid.’. To which point I would write an if statement to segregate the invalid card numbers. However, I am receiving this error:

const reversedArray = arr.reverse();
                           ^

TypeError: Cannot read property 'reverse' of undefined

Now, I understand what this error means. I just cannot figure out how to correctly pass the nested arrays within the batch array into the validateCred function. Any help is much appreciated! Thanks in advance :slight_smile:

I think you’re using .map incorrectly there, you wouldn’t call .map for each element in the array of cards

You could change the loop, so that inside the loop, you have
cardResults.push( validateCred(arr[x]) );
so that validateCred is executed for each element of array arr and the result is added to the cardResults array.
or
you could do replace the entire loop with a .map
let cardResults = arr.map(validateCred);

1 Like