Credit card checker - Step 4

I’m trying to do Step 4 but when I log the second function to the console, it’s giving me “undefined”. I just don’t understand why and I’ve been staring at my screen for almost an hour

// 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]


// Add your functions below:

test = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

const validateCred = array => {
    let lastDigit = array.pop()
    let newArray = array.reverse().map( (val, index) => {
        if (index % 2 === 0) {
            val *= 2
                return val > 9 ? val -=  9 : val;
        }  else {
            return  val
        } 
    })
    let sum = newArray.reduce( (a, b) => a + b, lastDigit )
    return sum % 10 === 0 ? 'Valid card number' : 'Invalid card number'

}

const findInvalidCards = nestedArray => {
    nestedArray.forEach(value => {
        validateCred(value)
        /*let invalidCards = [];

        if (validateCred(value) === 'Invalid card number') { 
            invalidCards.push(value) 
        }
        return invalidCards */
    }) 
} 


console.log(findInvalidCards(batch))

Hi,

Couple of things:

  • consider what purpose your functions serve. Are they returning data, changing data, displaying data, or some combination?

  • when you console.log a function invocation that doesn’t return data, you will get this undefined. If the function findInvalidCards() returns something, then you can log that.

  • consider using semi-colons at the end of your statements. It’s the opposite of python (where semicolons are frowned upon, even though legal to use). Here’s a discussion that talks about the merits.

Got it. Thank you!

One thing though, it’s returning 13 invalid cards. It should be only 8. Because there are 5 valid cards and 5 invalid cards. In the mystery cards, 3 cards are invalid so it should be a total of 8 invalid cards. I’m not sure why it’s returning 13

const findInvalidCards = nestedArray => {
    let invalidCards = [];
    let validCards = [];
    nestedArray.forEach(value => {
         validateCred(value)
        validateCred(value) === 'Invalid card number' ? invalidCards.push(value) : validCards.push(value)
    }) 
    return invalidCards;
} 


console.log(findInvalidCards(batch)) 

If I validate each one of the arrays, it shows 7 valid and 8 invalid cards

console.log(validateCred(valid1))
console.log(validateCred(valid2))
console.log(validateCred(valid3))
console.log(validateCred(valid4))
console.log(validateCred(valid5))
console.log(validateCred(invalid1))
console.log(validateCred(invalid2))
console.log(validateCred(invalid3))
console.log(validateCred(invalid4))
console.log(validateCred(invalid5)) 
console.log(validateCred(mystery1))
console.log(validateCred(mystery2))
console.log(validateCred(mystery3))
console.log(validateCred(mystery4))
console.log(validateCred(mystery5))

Found the problem, I commented out the

validateCred(value)

before the ternary operator

const findInvalidCards = nestedArray => {
    let invalidCards = [];
    let validCards = [];
    nestedArray.forEach(value => {
         //validateCred(value)
        validateCred(value) === 'Invalid card number' ? invalidCards.push(value) : validCards.push(value)
    }) 
    return invalidCards;
} 


console.log(findInvalidCards(batch)) 

Now it returns 8 invalid cards which is correct

Nice debugging!

1 Like

I’m currently working on this and I’m confused:
When logging my function: console.log(findInvalidCards(batch)); It just comes out as 8 arrays with a bunch of random numbers that don’t match any of the credit card arrays.
My code:

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


// Add your functions below:
function validateCred(array) {
  let newArray = [];
  for (let i = array.length - 1; i >= 0; i -= 1) {
    if (i % 2 === 0) {
      let newNum = array[i] *= 2;
      if (newNum > 9) {
        newNum -= 9;
      }
      newArray.push(newNum);
    } else {
      newArray.push(array[i]);
    }
  }
  const sum = newArray.reduce((accumulator, currentValue) => {  
    return accumulator + currentValue;
  });
  if (sum % 10 === 0) {
    return true;
  } else {
    return false;
  }
}

function findInvalidCards(nestArray) {
  let result = [];
  nestArray.forEach(value => {
    if (validateCred(value) === false) {
      result.push(value);
    }
  });
  return result;
}
console.log(findInvalidCards(batch));

I’m not sure what’s going on here.

You can debug this initially by checking this bit of code

let result = [];
  nestArray.forEach(value => {
    if (validateCred(value) === false) {
      result.push(value);
    }
  });

If in the forEach() method you log:

  • the item iterated
  • log validateCred(value) === false (which will tell you how it’s evaluating each item)

it’ll give you a clue as to whether you need to fix validateCred() or if it’s some other issue.

1 Like

Thank you for replying so soon. This is very helpful!

1 Like

I found that there is something wrong with my validateCred function
I logged all of the numbers and some of the valid ones were false, guess I should have done that from the start :stuck_out_tongue_closed_eyes:

That’s quite normal, little by little one builds intuition for this sort of thing.

2 Likes