Credit Card Checker

Hello!

I’m trying to do this project with lots of problems! First of all sorry for posting something that is already post, i’ve just read the answers that were already here and I can’t get the solution.

Could you please help me?

What is happening right now is that the code prints the arrays correctly but reversed and without one of the numbers, I think it has to be with the findInvalidCards fuction but I just can’t see it!

I’ll leave my code here:

function validateCred(tarjeta) {
  //estoy guardando el último número
  var ult
  ult = tarjeta[tarjeta.length -1]
  tarjeta.pop()
  tarjeta= tarjeta.reverse()
  var luhn = []
  var sum
  for (let i=0; i < tarjeta.length; i++) {
    if (i%2 != 0 ) {
      luhn [i] = tarjeta [i]
    }
    else {
      luhn [i] = tarjeta [i] * 2 
      if (luhn[i] > 9){
        luhn[i] = luhn [i] - 9
      }
    }
  }
sum = luhn.reduce((a,b)=>a+b)
sum = sum + ult
if (sum %10 ===0) {
  return true
}
  else {return false}
}
let invalidCards = []
function findInvalidCards(lista) {
  for (let j = 0; j < lista.length; j++) {
    if (validateCred(lista[j]) === false){
      invalidCards.push(lista[j])
    }
  }
}

findInvalidCards(batch)
console.log(invalidCards)

Thanks you !

Well this causes the last number to be removed from the array.

And as the assignment clearly states we are not allowed to modify the original array (card). Therefore we must first make a copy of the original array before operating the Luhn algorithm on it.

tarjeta = tarjeta.slice() // this makes a copy and stores it, then do the rest

Also I urge you to apply proper indentation, it seems to me that might clear up a few things as well.

PS: if you want the international audience to check your code, it may be wise to implement English comments and English variable names.

1 Like

Wow, that was huge and easy, you saved me.
I will definitely follow your advice

Thanks you so much !

1 Like

Sure, anytime, that’s what we are here for :wink: . Happy coding!