Files
30-seconds-of-code/snippets/luhnCheck.md
2018-01-03 17:56:42 +05:30

718 B

luhnCheck

Implementation of the Luhn Algorithm used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers etc. Works with numbers and strings alike

const luhnCheck = num => { 
    let arr = (num+'').split('').reverse()
    let lastDigit = arr.splice(0,1)
    let sum = arr.reduce((acc,val,i) => i%2!==0 ? acc + parseInt(val) : acc + (parseInt(val) * 2)%9 || 9,0)
     sum += parseInt(lastDigit)
     return sum%10 === 0
 }
 luhnCheck("4485275742308327"); //true
 luhnCheck(4485275742308327); //true
 luhnCheck(6011329933655299); // true
 luhnCheck(123456789); //false