Skip to content

Instantly share code, notes, and snippets.

@giautm
Last active April 18, 2016 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giautm/c9ce275fc677b3b31e6abfea04be9d78 to your computer and use it in GitHub Desktop.
Save giautm/c9ce275fc677b3b31e6abfea04be9d78 to your computer and use it in GitHub Desktop.
/**
* 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