Doodling with the Library Traveller script, I found that Blackwells use an ISBN 13 code in their book details page URLs, which the Amazon web service I use to look up book info (title, author etc.) doesn't much like: it wants ISBN 10.
Here's a first attempt at a javascript function to convert from ISBN13 to 10. Note I can't guarantee it's correct (though it seemed to work with a couple of test ISBNs I just tried).
function isbn13to10(isbn13) { var isbn9 = isbn13.substring(3,12); var n=0; var c=0; for (i=1;i<10;i++) { c = isbn9.substr(i,1) * 1; if ((c>0)&&(c<9)) n += (11-i)*c; } n = 11 - n%11; if (n==11) isbn10=isbn9+'0'; else if (n==10) isbn10=isbn9+'X'; else isbn10=isbn9+n; //alert(isbn10); return isbn10; }If you spot any flaws (quite likely, I was trying to translate a couple of functions from other languages and it's toooooo late... :-( please post a comment with a fix ;-) Posted by ajh59 at December 15, 2006 02:32 AM
I ran across your snippet, and promptly stole it for a similar function. However, I think you've got a fencepost error in the for loop, as my checksums kept coming out wrong. Anyway, I edited it as follows (i changed to dig because I was already using i) and it seems to work for me:
function isbn13to10(isbn13)
{
var isbn9 = isbn13.substring(3,12);
var n=0; var c=0;
for (dig=0;dig<9;dig++) {
c = isbn9.substr(dig,1) * 1;
n += (10-dig)*c;
}
n = 11 - n%11;
if (n==11) isbn10=isbn9+'0';
else if (n==10) isbn10=isbn9+'X';
else isbn10=isbn9+n;
//alert(isbn10);
return isbn10;
}
Hi Laura -
Thanks for that; I've kept meaning to run some proper tests and check that fn (it worked for a couple of random picks at the time, and went straight back on the 'fix when found to be broken' pile ;-)
I was also surprised at the time /not/ to find a set of proven js library functions out there to do this already?
--tony
Posted by: Tony Hirst at February 9, 2007 09:20 AM