diff --git a/tests/exercises/isbn-list/solution/solution.js b/tests/exercises/isbn-list/solution/solution.js new file mode 100644 index 00000000..fad85c78 --- /dev/null +++ b/tests/exercises/isbn-list/solution/solution.js @@ -0,0 +1,54 @@ +function isIsbn10(code) { + + function checkDigit(code) { + let check = 0; + for (let i = 0; i < 9; i++) { + check += parseInt(code[i]) * (i + 1); + } + check %= 11; + return check === 10 ? 'X' : check.toString(); + } + + if (code.length !== 10) { + return false; + } + + if (isNaN(Number(code.substring(0, 9)))) { + return false; + } + + return code[9] === checkDigit(code); +} + + +function isIsbn13(code) { + + function checkDigit(code) { + let check = 0; + for (let i = 0; i < 12; i++) { + check += parseInt(code[i]) * (i % 2 === 0 ? 1 : 3); + } + return ((((10 - check) % 10) + 10) % 10).toString(); + } + + if (code.length !== 13) { + return false; + } + + if (isNaN(Number(code.substring(0, 12)))) { + return false; + } + + return code[12] === checkDigit(code); +} + +function isIsbn(code, isbn13=true) { + return isbn13 ? isIsbn13(code) : isIsbn10(code); +} + +function areIsbn(codes, isbn13=undefined) { + if (isbn13 === undefined) { + return codes.map((code) => typeof code === 'string' ? isIsbn(code, code.length === 13) : false); + } + return codes.map((code) => typeof code === 'string' ? isIsbn(code, isbn13) : false); +} diff --git a/tests/exercises/isbn-list/solution/solution.ts b/tests/exercises/isbn-list/solution/solution.ts new file mode 100644 index 00000000..263bdc91 --- /dev/null +++ b/tests/exercises/isbn-list/solution/solution.ts @@ -0,0 +1,54 @@ +function isIsbn10(code: string): boolean { + + function checkDigit(code: string) { + let check: number = 0; + for (let i = 0; i < 9; i++) { + check += parseInt(code[i]) * (i + 1); + } + check %= 11; + return check === 10 ? 'X' : check.toString(); + } + + if (code.length !== 10) { + return false; + } + + if (isNaN(Number(code.substring(0, 9)))) { + return false; + } + + return code[9] === checkDigit(code); +} + + +function isIsbn13(code: string): boolean { + + function checkDigit(code: string) { + let check: number = 0; + for (let i = 0; i < 12; i++) { + check += parseInt(code[i]) * (i % 2 === 0 ? 1 : 3); + } + return ((((10 - check) % 10) + 10) % 10).toString(); + } + + if (code.length !== 13) { + return false; + } + + if (isNaN(Number(code.substring(0, 12)))) { + return false; + } + + return code[12] === checkDigit(code); +} + +function isIsbn(code: string, isbn13: boolean=true): boolean { + return isbn13 ? isIsbn13(code) : isIsbn10(code); +} + +function areIsbn(codes: Array, isbn13: boolean | undefined=undefined): Array { + if (isbn13 === undefined) { + return codes.map((code:unknown) => typeof code === 'string' ? isIsbn(code, code.length === 13) : false); + } + return codes.map((code:unknown) => typeof code === 'string' ? isIsbn(code, isbn13) : false); +} diff --git a/tests/exercises/isbn/evaluation/one-with-crashing-assignment-javascript.tson b/tests/exercises/isbn/evaluation/one-with-crashing-assignment-javascript.tson new file mode 100644 index 00000000..afc3b9b0 --- /dev/null +++ b/tests/exercises/isbn/evaluation/one-with-crashing-assignment-javascript.tson @@ -0,0 +1,88 @@ +{ + "tabs": [ + { + "name": "are_isbn", + "runs": [ + { + "contexts": [ + { + "before": { + "typescript": { + "data": "const ex = () => {throw new Error('AssertionError');};" + } + }, + "testcases": [ + { + "input": { + "type": "sequence", + "variable": "codes01", + "expression": { + "type": "function", + "namespace": "ex", + "name": "get", + "arguments": [] + } + } + }, + { + "input": { + "type": "function", + "name": "are_isbn", + "arguments": [ + "codes01" + ] + }, + "output": { + "result": { + "value": { + "data": [ + { + "data": false, + "type": "boolean" + }, + { + "data": true, + "type": "boolean" + }, + { + "data": true, + "type": "boolean" + }, + { + "data": true, + "type": "boolean" + }, + { + "data": false, + "type": "boolean" + }, + { + "data": false, + "type": "boolean" + }, + { + "data": false, + "type": "boolean" + }, + { + "data": true, + "type": "boolean" + }, + { + "data": false, + "type": "boolean" + } + ], + "type": "sequence" + } + } + } + } + ] + } + ] + } + ] + } + ] +} diff --git a/tests/exercises/isbn/solution/solution.js b/tests/exercises/isbn/solution/solution.js new file mode 100644 index 00000000..fad85c78 --- /dev/null +++ b/tests/exercises/isbn/solution/solution.js @@ -0,0 +1,54 @@ +function isIsbn10(code) { + + function checkDigit(code) { + let check = 0; + for (let i = 0; i < 9; i++) { + check += parseInt(code[i]) * (i + 1); + } + check %= 11; + return check === 10 ? 'X' : check.toString(); + } + + if (code.length !== 10) { + return false; + } + + if (isNaN(Number(code.substring(0, 9)))) { + return false; + } + + return code[9] === checkDigit(code); +} + + +function isIsbn13(code) { + + function checkDigit(code) { + let check = 0; + for (let i = 0; i < 12; i++) { + check += parseInt(code[i]) * (i % 2 === 0 ? 1 : 3); + } + return ((((10 - check) % 10) + 10) % 10).toString(); + } + + if (code.length !== 13) { + return false; + } + + if (isNaN(Number(code.substring(0, 12)))) { + return false; + } + + return code[12] === checkDigit(code); +} + +function isIsbn(code, isbn13=true) { + return isbn13 ? isIsbn13(code) : isIsbn10(code); +} + +function areIsbn(codes, isbn13=undefined) { + if (isbn13 === undefined) { + return codes.map((code) => typeof code === 'string' ? isIsbn(code, code.length === 13) : false); + } + return codes.map((code) => typeof code === 'string' ? isIsbn(code, isbn13) : false); +}