Skip to content

Instantly share code, notes, and snippets.

@tsupo
Created May 8, 2009 18:43
Show Gist options
  • Save tsupo/108922 to your computer and use it in GitHub Desktop.
Save tsupo/108922 to your computer and use it in GitHub Desktop.
covert code between ISBN10 and ISBN13
/* convISBN.js : converter ISBN10 <-> ISBN13 */
/* Copyright (c) 2007 by H.Tsujimura <[email protected]> */
/* Distributed by LGPL. */
/* this script written by H.Tsujimura 20 Jan 2007 */
function convISBN13toISBN10(str) {
var s;
var c;
var checkDigit = 0;
var result = "";
s = str.substring(3,str.length);
for ( i = 10; i > 1; i-- ) {
c = s.charAt(10 - i);
checkDigit += (c - 0) * i;
result += c;
}
checkDigit = (11 - (checkDigit % 11)) % 11;
result += checkDigit == 10 ? 'X' : (checkDigit + "");
return ( result );
}
function convISBN10toISBN13(str) {
var c;
var checkDigit = 0;
var result = "";
c = '9';
result += c;
checkDigit += (c - 0) * 1;
c = '7';
result += c;
checkDigit += (c - 0) * 3;
c = '8';
result += c;
checkDigit += (c - 0) * 1;
for ( i = 0; i < 9; i++ ) { // >
c = str.charAt(i);
if ( i % 2 == 0 )
checkDigit += (c - 0) * 3;
else
checkDigit += (c - 0) * 1;
result += c;
}
checkDigit = (10 - (checkDigit % 10)) % 10;
result += (checkDigit + "");
return ( result );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment