idInvalidCardCompanies()

Hi there,
When I go to execute this piece of code for my idInvalidCardCompanies() function for the Credit Card Checker, I get an empty array instead of an array of the companies. I’m not sure what the bug in my code is.

function idInvalidCardCompanies(nestedInvalid) {
  let companies = [];
  for (let i = 0; i < nestedInvalid.length; i++) {
    if (nestedInvalid[i][0] === 3) {
      companies.push('Amex (American Express)')
    } else if (nestedInvalid[i][0] === 4) {
      companies.push('Visa') 
    } else if (nestedInvalid[i][0] === 5) {
      companies.push('MasterCard') 
    } else if (nestedInvalid[i][0] === 6) {
      companies.push('Discover')
    } else {
      companies.push('Company not found')
    };
  };
  return companies;
};

thanks,

Can you copy paste the whole code by chance?

const reduce = (accumateValue, currentValue) => accumateValue + currentValue;

function validCred(array) {
    // saves original array
    const arrayOne = array.splice();
  // doubling loop
  for (let i = array.length - 1; i >= 1; i-=2) {
    array[i] = array[i] * 2
    if (array[i] > 9) {
      array[i] -= 9
    };
  };
  // reduce adds up all numbers in array
  const arrayTwo = array.reduce(reduce)
  const remainder = arrayTwo % 2;
  if (remainder === 0) {
    return true;
  } else if (remainder != 0){
    return false;
  }
};

let invalidCards = [];

function findInvalidCards(nestedArr) {
    let i = 0;
    while (i < nestedArr.length) {
        if (validCred(nestedArr[i]) !== true) {
            invalidCards.push(nestedArr[i]);
        } 
        i++
    }
    return invalidCards;
};

// console.log(findInvalidCards(batch))

function idInvalidCardCompanies(nestedInvalid) {
  let companies = [];
  for (let i = 0; i < nestedInvalid.length; i++) {
    if (nestedInvalid[i][0] === 3) {
      companies.push('Amex (American Express)')
    } else if (nestedInvalid[i][0] === 4) {
      companies.push('Visa') 
    } else if (nestedInvalid[i][0] === 5) {
      companies.push('MasterCard') 
    } else if (nestedInvalid[i][0] === 6) {
      companies.push('Discover')
    } else {
      companies.push('Company not found')
    };
  };
  return companies;
};

console.log(idInvalidCardCompanies(invalidCards));

not sure if this is what you mean but this is my whole project so far

Hey bit8141287786

invalidCards is empty:

let invalidCards = [];

Is filled in by calling the function findInvalidCards(), so you gotta call this function before calling any other function that may require its output. However you commented it out:

// console.log(findInvalidCards(batch)); 

So it’s never called before:

console.log(idInvalidCardCompanies(invalidCards));

Passing in an empty array.

oh great! thanks @usernamegiapreso for the help

1 Like