Credit Card Checker Assignment | Step 4

Hello,

I have recently been working on the Credit Card Checker Assignment and I have gotten stuck on Step 4. I feel like my logic is correct, but when I try to test my “findInvalidCards” function, it shows nothing for about 20 seconds and then does not display any results (there are no errors from calling it either).

I believe I have completed step 3 correctly, but that may be the root of the issue. However, when I have tested the function it works correctly to detect if the individual arrays are valid or invalid, i.e 8 invalid numbers.

Step 3 | Create the validateCred() function

const validateCred = array => {
  let doubleCheck = []
  for (i = array.length-2; i>=0; i-= 2) {
    let double = array[i]*2
    if (double > 9) {
      doubleCheck.push(double-9)
    } else {
      doubleCheck.push(double)
    }
  }

  let remainingNum = []
  for (i = array.length-1; i>=0; i-= 2) {
    remainingNum.push(array[i])
  }

  let allDigits = doubleCheck.concat(remainingNum)

  let sum = 0
  allDigits.forEach(num => {
    sum += num
  })

  if (sum % 10 == 0) {
    return 'valid'
  } else {
    return 'invalid'
  }
}

Step 4 | Create the findInvalidCards() functionWhere I am getting stuck!

const findInvalidCards = cardNumbers => {
  let invalidCards = []
  for (i=0;i<cardNumbers.length;i++) {
    let check = validateCred(cardNumbers[i])
    if (check == 'invalid') {
      return invalidCards.push(check)
    }
  }
  console.log(invalidCards)
}

I try to print the results and call the function on batch, i.e findInvalidCards(batch).

Any advice/tips would be appreciated!

return and array.push() are rarely seen in the same statement, and return even less so inside a loop.


About Array.push()

The method does what it is expected to do, add an element to the end of an array, but it also has a return value, the new array length. So it’s not completely unlikely that we might return the return value of .push, but remote, just the same.

Hey, I actually forgot to remove the return from the code, that was my mistake. Thanks for catching that!

However, it still does not work.

You have a return inside of the loop in the findInvalidCards function.
If it gets an invalid card, you return something, so the function ends.
You want the loop (and the function) to keep going instead, so that return shouldn’t be there.

Hey, thanks for taking a look. I have already tried it without the return and still run into the same problem.

It worked after I changed your loop in the findInvalidCards function slightly:
I changed

for (i=0;i<cardNumbers.length;i++) {

to

for (let i=0;i<cardNumbers.length;i++) {

since the variable i was not declared with a let or var or something.

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];


// Step 3 | Create the validateCred() function

const validateCred = array => {
  let doubleCheck = []
  for (i = array.length-2; i>=0; i-= 2) {
    let double = array[i]*2
    if (double > 9) {
      doubleCheck.push(double-9)
    } else {
      doubleCheck.push(double)
    }
  }

  let remainingNum = []
  for (i = array.length-1; i>=0; i-= 2) {
    remainingNum.push(array[i])
  }

  let allDigits = doubleCheck.concat(remainingNum)

  let sum = 0
  allDigits.forEach(num => {
    sum += num
  })

  if (sum % 10 == 0) {
    return 'valid'
  } else {
    return 'invalid'
  }
}


// Step 4 | Create the findInvalidCards() function

const findInvalidCards = cardNumbers => {
  let invalidCards = []
  for (let i=0;i<cardNumbers.length;i++) {
    let check = validateCred(cardNumbers[i])
    if (check == 'invalid') {
      invalidCards.push(check)
    }
  }
  console.log(invalidCards)
}

// test Step 4:

findInvalidCards(batch);

Also, in that loop, it may be more useful to push cardNumbers[i] to the invalidCards array instead of pushing check.
And you may want that function to return invalidCards if you want to use the results of the function somewhere else in your code.

1 Like

Oh. My. Goodness. It always is the little things that get me, ugh. Thank you so much, I have been stuck on this for 3 days.

In the meantime, I had changed it to push directly into the invalidCards array. Thanks again!