Credit card checker project

Hello! :slight_smile:

When i run this function, if it is in a single array of numbers, it works out properly, but if i run it in a loop to check a array with arrays inside, it simply loops forever and i can’t understand why

const batch = [valid1, valid2, valid3, valid4]

validateCred = (array) => {

    var count = 0;

    var soma = 0;

    for (i=array.length - 1; i != -1; i--) {

        if (count === 0) {

            count += 1;

            soma += array[i];

        } else {

            if(array[i]*2 > 9) {

                soma += array[i]*2 - 9;

            } else {

                soma += array[i]*2;

            }

            count = 0

        }

    }

    if (soma % 10 === 0) {

        return true

    } else {

        return false

    }

}

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

     console.log(validateCred(batch[i]))

}
for (i=0; i < batch.length; i++) 

always use let or const to declare variables to avoid these errors.
so change it to:

for (let i=0; i < batch.length; i++) {
1 Like

i can’t believe i wasted 2h trying to solve it, and i couldn’t find this XD
thanks!

Don’t worry that’s normal we all make a lot of errors :D. Also, you should stop using var for many reasons read this article if you want to know these reasons.
so go ahead and change var to let or const in your code!

3 Likes