Chalarangelo / 30-seconds-of-code

Short code snippets for all your development needs

Home Page:https://30secondsofcode.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong result for some inputs

tomer-gavish opened this issue · comments

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.