-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c5fb18
commit 07c0709
Showing
4 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
88 changes: 88 additions & 0 deletions
88
tests/exercises/isbn/evaluation/one-with-crashing-assignment-javascript.tson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |