An ISBN validation function.
Inspiration for this came from: http://java.dzone.com/articles/thursday-code-puzzler-isbn
An ISBN validation function.
Inspiration for this came from: http://java.dzone.com/articles/thursday-code-puzzler-isbn
| 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> |
even shorter: