I have an issue with my code, it seems to work but when I test the function with different arrays, the result always shows false, even when I test the function with a valid array. I think my istake is with the algorithm, but i’m not sure. Can someone help explain my mistake? Thanks in advance!
function validateCred(card){
let newCard = card.splice().reverse();
for(let i = 0; i < card.length; i++){
if(i % 2 != 0){
newCard[i] *= 2;
if(newCard[i] > 9){
newCard -= 9);
}
}
}
let total = newCard.reduce((a,b) => a + b, 0);
if(total % 10 === 0){
return true;
} else {
return false;
}
};
console.log(validateCred(valid1));