Credit Card Checker - findInvalidCards() returning reversed cc numbers

I’ve got my validateCred function working great. But when I run my findInvalidCards function, it does put valid card numbers in one new array and invalid in another, but all the numbers are reversed and I can’t figure out why. The numbers do get reversed in validateCred, but as a new variable (reverseArray).

// Add your functions below:
const validateCred = array => {
  let reverseArray = array.reverse();
  for (let i = 0; i < reverseArray.length; i++) {
    if (i % 2 !== 0) {
      reverseArray[i] = reverseArray[i] * 2;
    } if (reverseArray[i] > 9) {
      reverseArray[i] -= 9
    }
   }
   let sum = 0;
   for (let x = 0; x < reverseArray.length; x++) {
     sum = sum + reverseArray[x];
   }
   if (sum % 10 === 0) {
     return 'valid';
   } else {
     return 'invalid';
   }
}

//console.log(validateCred(mystery5));

const findInvalidCards = nested => {
  let validNumbers = [];
  let invalidNumbers = [];
  for (let i = 0; i < nested.length; i++) {
    if (validateCred(nested[i]) === 'valid') {
      validNumbers.push(nested[i]);
    } else {
      invalidNumbers.push(nested[i]);
    }
  }
  console.log(validNumbers);
  console.log(invalidNumbers);
}

findInvalidCards(batch)

which returns the credit card numbers reversed AND already put through the Luhn algorithm calculation.

For example…
It’s pulling ‘8, 0, 8, 3, 1, 0, 8, 0, 9, 5, 7, 3, 9, 6, 5, 8’ when it
should be ‘4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8’.

Please help

1 Like

In the validateCred function,
array.reverse reverses the original array
(.reverse reverses the original array because an array is a kind of object.)
You’d have to make a copy of the array, and then reverse it.

One way to make a copy of an array is to use Array.from( )
let reverseArray = Array.from(array).reverse();

1 Like