dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
1.5 KiB
1.5 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69e2383af7832c8032603b8f | Challenge 273: ISBN-13 Validator | 28 | challenge-273 |
--description--
Given a string, determine if it is a valid ISBN-13 number.
A valid ISBN-13:
- Contains only digits and hyphens
- Has exactly 13 digits after removing hyphens
- Passes the following check:
- Multiply each digit by 1 or 3, alternating (multiply the first digit by 1, the second by 3, the third by 1, and so on).
- The sum of the results must be divisible by 10.
--hints--
isValidIsbn13("9780306406157") should return true.
assert.isTrue(isValidIsbn13("9780306406157"));
isValidIsbn13("97803064061570") should return false.
assert.isFalse(isValidIsbn13("97803064061570"));
isValidIsbn13("978-0-13-595705-9") should return true.
assert.isTrue(isValidIsbn13("978-0-13-595705-9"));
isValidIsbn13("978-030-64061A-4") should return false.
assert.isFalse(isValidIsbn13("978-030-64061A-4"));
isValidIsbn13("9-7-8-0-1-3-4-7-5-7-5-9-9") should return true.
assert.isTrue(isValidIsbn13("9-7-8-0-1-3-4-7-5-7-5-9-9"));
--seed--
--seed-contents--
function isValidIsbn13(str) {
return str;
}
--solutions--
function isValidIsbn13(str) {
const digits = str.replace(/-/g, '');
if (!/^\d{13}$/.test(digits)) return false;
const sum = digits.split('').reduce((acc, d, i) => acc + d * (i % 2 === 0 ? 1 : 3), 0);
return sum % 10 === 0;
}