Credit Card Checker - Javascript Project

I’ve struggled through this project but making some headway, however I’m stumped now as when I come to check through the invalid cards from the nested array, the invalid5 seems to be returning as true rather than false and I can’t figure out why.

I’ve added another batch array so I could figure out which one was not working properly.

const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];

const batch1 = [invalid1, invalid2, invalid3, invalid4, invalid5]

// Add your functions below:
function validateCred(array) { 
  var lastDigit = array.splice(array.length - 1, array.length);
  let total = 0;
  for (let i = 0; i < array.length; i++) {
    let number = array[i];
    if(i % 2 === 0){
      number = number * 2;
        if (number > 9) {
            number -= 9;     
            total = total + number;
          }   else {      
                total = total + number;     
    } else {
        total = total + number;
  lastDigit = Number(lastDigit);
  total += lastDigit;
  if (total % 10 === 0){
    return true;
  } else {
    return false;


const findInvalidCards = array => {
  let failedCards = []
  for (let i = 0; i < array.length; i++)
    if ( validateCred(array[i]) ) {
      console.log('Not working');
    } else {


Well, how about that. I haven’t run any test but, reading your code I make the following assumption.

The fault lies in your validateCred function. Any operation on the original array modifies it for the next operation. The exercise stated that modification of the original is not allowed and this is an example of why.

Since you are running invalid5 to test your function before running the batch function the invalid5 array has been altered, and as luck has it, into a ‘correct’ array. Thus causing findInvalidCards to return a true on that card.

The way to solve this is to make a copy of the original array before operating on it. You do this by defining a new variable and calling slice on the array.

let arrayCopy = array.slice();
var lastDigit = arrayCopy.splice(arrayCopy.length - 1, arrayCopy.length);
for (let i = 0; i < arrayCopy.length; i++) {
    let number = arrayCopy[i];
    //etc etc

Nice case of debugging :wink:

Happy coding!