Skip to content

Instantly share code, notes, and snippets.

@tkissing
Forked from 140bytes/LICENSE.txt
Created November 7, 2011 04:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkissing/1344175 to your computer and use it in GitHub Desktop.
Save tkissing/1344175 to your computer and use it in GitHub Desktop.
JavaScript Luhn Checksum 140byt.es

luhn

Implementation of the Luhn checksum algorithm, see http://en.wikipedia.org/wiki/Luhn_algorithm

First argument should be a number or a string composed of digits only. Second argument is optional. If second argument evaluates to true, the return value is the concatenation of the first argument and its Luhn checksum. Otherwise the return value is a boolean indicating if the first arguments checksum digit is valid.

/*
* @param b Number to validate or add checksum digit to, as Number or as String
* @param y optional; if true, add checksum digit to b, otherwise validate
* @param t placeholder
* @param e placeholder
* @param s placeholder
* @param u placeholder
*/
function(b, y, t, e, s, u) {
// initialize sum s=0 and multiplicator
s = 0; u = y ? 1 : 2;
// ensure b is a string and iterate over digits in b from right to left
for (t = ( b = b + '').length; t--;) {
e = b[t] * (u^=3); // if we are at an "even" position, double the digit
s += e-(e>9?9:0); // reduce to one digit and add up
}
t = 10 - (s % 10 || 10); // calculate the difference to modulo10
return y ? b + t : !t; // if calculating checksum, concat; if validating, 0 is a pass
}
function(b,y,t,e,s,u){s=0;u=y?1:2;for(t=(b=b+'').length;t--;){e=b[t]*(u^=3);s+=e-(e>9?9:0)}t=10-(s%10||10);return y?b+t:!t}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Timo Kissing http://kissing.name
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "jsLuhn",
"description": "Implementation of the Luhn checksum algorithm",
"keywords": [
"140bytes",
"number",
"luhn",
"checksum"
]
}
<!DOCTYPE html>
<head>
<title>140byt.es Luhn Checksum</title>
</head>
<body>
<div>Expected value: <b>true,49927398716, true</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var luhn = function(b,y,t,e,s,u){s=0;u=y?1:2;for(t=(b=b+'').length;t--;){e=b[t]*(u^=3);s+=e-(e>9?9:0)}t=10-(s%10||10);return y?b+t:!t};
document.getElementById( "ret" ).innerHTML = [luhn(49927398716), luhn('4992739871', true), luhn('4111111111111111')];
</script>
</body>
</html>
@jed
Copy link

jed commented Nov 7, 2011

@tkissing
Copy link
Author

tkissing commented Nov 7, 2011 via email

@tsaniel
Copy link

tsaniel commented Nov 12, 2011

I just golfed the validate version, which is with maths so it should be fast.

function(a,b,c,d){for(a*=b=c=10;a/=10;c+=b++%2?d*2-(d>4)*9:d)a-=d=a%10;return!(c%10)}

Maybe you can make it suit you;)

@tsaniel
Copy link

tsaniel commented Nov 12, 2011

However, I found that your version has some problems.
For example, testing 4111111111111111 should be true, you version says false.

@tkissing
Copy link
Author

Doh, you are right @tsaniel.
Have a fix, but need to update annotated version so will just put it in the comment here for now:
function(b,y,t,e,s,u){s=0;u=y?1:2;for(t=(b=b+'').length;t--;){e=b[t]*(u^=3);s+=e-(e>9?9:0)}t=10-(s%10||10);return y?b+t:!t}
And yes, that is 1 byte shorter than the broken version

@tkissing
Copy link
Author

And fixed.

@jed
Copy link

jed commented Nov 13, 2011

e-(e>9?9:0) => e-9*(e>9)
y?1:2 => 2-!y
b=b+'' => b+=''
;){X;Y} => ;Y)X;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment