Last active
April 18, 2016 10:55
-
-
Save giautm/c9ce275fc677b3b31e6abfea04be9d78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Return true for a valid IMEI number. | |
* | |
* @param string str - String to check | |
*/ | |
function checkLuhn(str, chk) { | |
var tmp = str.split(''); | |
var sum = 0; | |
chk = parseInt(chk || tmp.pop()); | |
tmp.forEach(function (num, idx) { | |
num = parseInt(num) << (idx & 1); | |
sum += ~~(num / 10) + (num % 10); | |
}); | |
return (sum + chk) % 10 === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment