diff --git a/modules-examples/books.js b/modules-examples/books.js new file mode 100644 index 0000000..6f78ebd --- /dev/null +++ b/modules-examples/books.js @@ -0,0 +1,48 @@ +export function isValidIsbn13(str) { + let sum, digit, check, i; + + str = str.replace(/[^0-9X]/gi, ''); + + if (str.length !== 13) { + return false; + } + sum = 0; + for (i = 0; i < 12; i++) { + digit = parseInt(str[i]); + if (i % 2 === 1) { + sum += 3 * digit; + } else { + sum += digit; + } + } + check = (10 - (sum % 10)) % 10; + return (check === str[str.length - 1]); +} + +export function isValidIsbn10(str) { + let sum, digit, weight, check, i; + + str = str.replace(/[^0-9X]/gi, ''); + + if (str.length !== 10) { + return false; + } + weight = 10; + sum = 0; + for (i = 0; i < 9; i++) { + digit = parseInt(str[i]); + sum += weight * digit; + weight--; + } + check = (11 - (sum % 11)) % 11 + if (check === 10) { + check = 'X'; + } + return (check === str[str.length - 1].toUpperCase()); + +} + + +export function isValidIsbn(str) { + return isValidIsbn13(str) || isValidIsbn10(str); +} \ No newline at end of file diff --git a/modules-examples/stats.js b/modules-examples/stats.js index 81c07fb..8ceb4bd 100644 --- a/modules-examples/stats.js +++ b/modules-examples/stats.js @@ -1,3 +1,5 @@ + +export const INFINITY = 30; export function average(lst){ if(lst.length < 1){ return NaN;