 # Help With Credit Card Checker Project

I feel like I’m going crazy, I’m on the Credit Card Checker for JavaScript, and I made this code:

``````const validateCred = (arr) => {
let check = arr[arr.length-1];
let arrcopy = arr.map((x) => x);
arrcopy.pop();
arrcopy.reverse();
for (let i = 0; i < arr.length; i = i+2){
arrcopy[i] = arrcopy[i]*2;
if (arrcopy[i] > 9){
arrcopy[i] = arrcopy[i]-9
}else{
arrcopy[i] = arrcopy[i];
}
};
var sum = arrcopy.reduce(function(a, b){
return a + b;
}, 0);
let totalSum = sum + check;
if (totalSum % 10 === 0){
return true;
}else{
return false;
};
};
``````

This algorithm works for every valid card number except for this one;

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

I’ve tried to calculate by hand whether this is a valid credit card number or not, and so far I have come back with a negative. Can I please have some outer feedback suggesting what I do, unless this is a content error.

Thanks in advance!

The algorithm needs to be able to work on any length of CC number. Not all are 16 digits. Consider also that for odd lengths, the first number to get doubled is index 1. It is index 0 for even lengths.

1 Like

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