Wrong result for some inputs
tomer-gavish opened this issue · comments
Tomer Gavish commented
In case a doubled digit equals to 0, the sum is miscalculated and the result is wrong.
For example, the following valid number returns false
instead of true
:
luhnCheck('4024007174941954');
Or a shorter version:
luhnCheck('406'); // false, but should be true
A possible solution would be to replace the reducer function with this:
(acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val))
The fix has been tested against all Paypal's credit card numbers for testing.