findInvalidCards()

Hi! :smiley:

I am stuck on this exercise, the complete function is here https://codepen.io/marianpractica/pen/XWNmOyR?editors=0012

i was reading other answers but i couldn’t understand my logic mistake of maybe i should rewrite and change my approach. Could you help me please? Thanks a lot! :smiley:

const findInvalidCards = (...array) => {
  let invalidCards = [];
  const [...rest] = [array];
  for (let array of rest) {
      console.log(array);
      if (validateCred(array)) {
      invalidCards.push(array)
    }
    
  }
    return invalidCards;
}

Hey @mariangz
without fully understanding the exercise and your functions, I assume you want to print 2 arrays to the console – those of the invalid cards, right?
In that case, I see two issues here.

  1. You use the spread syntax to pass the arguments to the function findInvalidCards. It gets to the function as one array containing three arrays. That means your for loop runs only once.
    If you rebuild it like this, the console returns the array of the valid card:
const findInvalidCards = (...array) => {
  let invalidCards = [];
  const [...rest] = [array];
  for (let i = 0; i < rest[0].length; i++) {
    if (validateCred(rest[0][i])) {
      invalidCards.push(rest[0][i]);
    }
    
  }
    return invalidCards;
}
  1. If you want to return the invalid card arrays, change the if condition to this:
if (!validateCred(rest[0][i]))
1 Like

thanks soooooo much!!!

1 Like