Skip to content

Commit

Permalink
add test files
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentBlanckaert committed Nov 14, 2024
1 parent 0c5fb18 commit 07c0709
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/exercises/isbn-list/solution/solution.js
Original file line number Diff line number Diff line change
@@ -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);
}
54 changes: 54 additions & 0 deletions tests/exercises/isbn-list/solution/solution.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>, isbn13: boolean | undefined=undefined): Array<boolean> {
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);
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
]
}
]
}
]
}
]
}
54 changes: 54 additions & 0 deletions tests/exercises/isbn/solution/solution.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 07c0709

Please sign in to comment.