I believe this is a scope problem, but idk why it's not working. I'm fairly new to js, tried checking the documentation but still couldn't solve it
For some reason, the "soma" variable always logs as "NaN" when inside the for loop. Prob is something simple, but i've been hitting my head on the desk for almost an hour now. Help D:
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]
// Luhn Algorithm
validateCred = (array) => {
var count = 0
var soma = 0
for (i=array.length; 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
}
console.log(soma%10) // expected output === 0
}
}
validateCred(valid1)