invalidCards is not defined?

// Add your functions below:
function validateCred(number) {
    let cardInverted = number.slice().reverse();

    for (let i=0; i < cardInverted.length; i++) { 
      if (i % 2 != 0) {
        cardInverted[i] = cardInverted[i] * 2;
        if (cardInverted[i] > 9) {
          cardInverted[i] -= 9;
        }  
      }
    }

    let sum = cardInverted.reduce((a, b) => a + b, 0);
    if (sum % 10 == 0) {
      return true;
    } else {
      return false;
    }  
  }


function findInvalidCards(batch) {
    let invalidCards = [];

    for (let j = 0; j < batch.length; j++) {
        validateCred(batch[j]);
        if (validateCred(batch[j]) === false) {
            invalidCards.push(batch[j]);
        }
    }
    return invalidCards;
}

function idInvalidCardCompanies(invalidCards) {
    let companies = [];

    for (let k = 0; k < invalidCards.length; k++) {
        if (invalidCards[k][0]=== 3 && companies.indexOf('American Express') === -1) {
            companies.push('American Express');
        } else if (invalidCards[k][0]=== 4 && companies.indexOf('Visa') === -1) {
            companies.push('Visa');
        } else if (invalidCards[k][0]=== 5 && companies.indexOf('Mastercard') === -1) {
            companies.push('Mastercard');
        } else if (invalidCards[k][0]=== 6 && companies.indexOf('Discover') === -1) {
            companies.push('Discover');
        } else {
            console.log('Company not found');
        }
    }
    return companies;
}
 console.log(invalidCards);

When I attempt to log invalidCards at the end, it claims it is not defined but for the life of me I can’t find the issue? Is the array not defined in the findInvalidCards function? Or is the fact that it’s defined within this function preventing it from being accessed from outside the function?

Any help would be hugely appreciated!

I’m not really sure what you are trying to do, you defined some functions and try to console.log() empty list out of one of them without calling a function with appropriate argument? What you should do is:

let result = findInvalidCards(batch)
console.log(result)

But you have to provide batch. Return does not make your invalidCards visible for global scope. Your variable is defined inside function’s scope. You could also define variable outside function and then inside it assign value to it:

var invalidCards

function findInvalidCards(batch) {
    invalidCards = [];

    for (let j = 0; j < batch.length; j++) {
        validateCred(batch[j]);
        if (validateCred(batch[j]) === false) {
            invalidCards.push(batch[j]);
        }
    }
    return invalidCards;
}
findInvalidCards(SOMEARGUMENT)
console.log(invalidCards) --> should return some kind of value

But again for it to take effect you must call a function and provide it with correct arguments.

I hope i answered your question. It’s just ambiguous what you are asking for.

1 Like

Thank you so much, I realised after posting that I didn’t do a great job of explaining what I was trying to do. I managed to solve the issue in exactly the manner you mentioned, but having it all written it out in your answer has clarified it and made it far more understandable for me!