I'm trying to port a Luhm algorithm implementation I have that's in C# to JavaScript. I got it ported over and I thought it was working, but I can't get legitimate cards to validate. My AMEX validates fine, but my two VISAs refuse to do so. Here's the code:
luhn = function (number) {
var _deltas = [0, 1, 2, 3, 4, -4, -3, -2, -1, 0],
_checksum = 0,
_digits = [],
i,
j;
while (number != 0) {
_digits.push(parseInt((number % 10), 10));
number = parseInt((number / 10), 10);
}
for (i = (_digits.length - 1), j = _digits.length; i > -1; i--) {
_digit = _digits[i];
_checksum += _digit;
_checksum += ((((i - j) % 2) === 0) ? _deltas[_digit] : 0);
}
return ((_checksum % 10) === 0);
};
Can someone point me in the right direction on what's wrong? I thought this worked fine in the C# version, but now I'm having doubts... Thanks in advance!