Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Created March 2, 2016 22:31
Show Gist options
  • Save DanielFGray/184e7c2eb991c32f20cc to your computer and use it in GitHub Desktop.
Save DanielFGray/184e7c2eb991c32f20cc to your computer and use it in GitHub Desktop.
'use strict';
function validISBN10(isbn) {
if(! /\d{9}[X0-9]/.test(isbn)) return false;
return isbn.split('').reduce((p, c, i) => {
return p + ((c == 'X') ? 10 : parseInt(c)) * ++i;
}, 0) % 11 == 0;
}
(function (func, cases, invert) {
cases.forEach(t =>
console.log(func(t[0]) == t[1], `${func.name}(${t[0]}) == ${t[1]}`)
);
})(validISBN10, [
[ '1112223339', true ],
[ '1234554321', true ],
[ '1234512345', false ],
[ '048665088X', true ]
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment