Skip to content

Instantly share code, notes, and snippets.

@mrpants
Forked from 140bytes/LICENSE.txt
Last active May 6, 2021 15:16
Show Gist options
  • Save mrpants/5396995 to your computer and use it in GitHub Desktop.
Save mrpants/5396995 to your computer and use it in GitHub Desktop.
ISBN Validation
function v(
a, // ISBN number to validate
b, // sum of digits (for ISBN algorithm) (placeholder)
c, // counter (placeholder)
d // current digit (placeholder)
){
for(
b = 0, // initialize sum
c = a.length-1; // skip the last character of the string (check digit)
c--; // work through the string backwards
)
d = +a[c],
b += // sum for ISBN algorithm..
c % 2 ? // if the counter is even or 0..
d * 1 // add the current digit times 1 to the sum
: // else
d * 3; // add the current digit times 3 to the sum
return +a[a.length-1] == 10 - b % 10 // return true if the last digit equals
// the calculated check digit
// (10 minus sum mod 10)
}
function v(a,b,c,d){for(b=0,c=a.length-1;c--;)d=+a[c],b+=c%2?d*1:d*3;return+a[a.length-1]==10-b%10}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
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": "isbnValidator",
"description": "An ISBN number validation function.",
"keywords": [
"isbn",
"validation"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>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 myFunction = function(a,b,c,d){for(b=0,c=a.length-1;c--;)d=+a[c],b+=c%2?d*1:d*3;return+a[a.length-1]==10-b%10}
document.getElementById( "ret" ).innerHTML = myFunction("9780306406157")
</script>
@openorclose
Copy link

shortened:

function(a,b,c){for(b=c=0;a[c]&&(b+=a[c++]*(c%2?1:3)););return!(b%10)}

@atk
Copy link

atk commented Apr 24, 2013

even shorter:

function (a,b,c){b=0;for(c in a)b+=a[c]*(c%2?3:1);return!(b%10)}

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