Credit Card Checker

Can someone point out why does it return false?

const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];

function validateCred(arr){
let newArr = ;
let sum = 0;
for(let i = arr.length - 1; i >= 0; iā€“){
if(i % 2 === 0){
if(arr[i] * 2 > 9){
newArr.push(arr[i] * 2 - 9);
}else{newArr.push(arr[i] * 2)}
}else{newArr.push(arr[i])}
}
newArr.forEach(i => {sum += i;});
if(sum % 10 === 0){return true}else{return false}
// (sum === 0) ? true : false;
}

It has to do with the length of that credit card number. valid3.length is 15 so when you run your for loop i is 14, that causes your if(i % 2 === 0) to be true.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.