Skip to content

Instantly share code, notes, and snippets.

@kaorimatz
Created January 25, 2013 15:00
Show Gist options
  • Save kaorimatz/4635045 to your computer and use it in GitHub Desktop.
Save kaorimatz/4635045 to your computer and use it in GitHub Desktop.
function toIsbn13(isbn10) {
var digits = '978' + isbn10.substr(0, 9);
var tmp = digits.split('').map(function (e) {
return parseInt(e);
}).map(function (e, i) {
return (i % 2 == 0) ? e : e * 3;
}).reduce(function (a, b) {
return a + b;
}) % 10;
var checksum = tmp == 0 ? 0 : 10 - tmp;
return digits + checksum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment