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.8 KiB
1.8 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69b1028d6e265413d0198a2a | Challenge 231: ISBN-10 Validator | 28 | challenge-231 |
--description--
Given a string, determine if it's a valid ISBN-10.
An ISBN-10 consists of hyphens ("-") and 10 other characters. After removing the hyphens ("-"):
- The first 9 characters must be digits, and
- The final character may be a digit or the letter
"X", which represents the number 10.
To validate it:
- Multiply each digit (or value) by its position (multiply the first digit by 1, the second by 2, and so on).
- Add all the results together.
- If the total is divisible by 11, it's valid.
--hints--
isValidIsbn10("0-306-40615-2") should return true.
assert.isTrue(isValidIsbn10("0-306-40615-2"));
isValidIsbn10("0-306-40615-1") should return false.
assert.isFalse(isValidIsbn10("0-306-40615-1"));
isValidIsbn10("0-8044-2957-X") should return true.
assert.isTrue(isValidIsbn10("0-8044-2957-X"));
isValidIsbn10("X-306-40615-2") should return false.
assert.isFalse(isValidIsbn10("X-306-40615-2"));
isValidIsbn10("0-6822-2589-4") should return true.
assert.isTrue(isValidIsbn10("0-6822-2589-4"));
--seed--
--seed-contents--
function isValidIsbn10(str) {
return str;
}
--solutions--
function isValidIsbn10(str) {
const isbn = str.replace(/-/g, "");
if (isbn.length !== 10) return false;
let sum = 0;
for (let i = 0; i < 9; i++) {
const digit = Number(isbn[i]);
if (Number.isNaN(digit)) return false;
sum += digit * (i + 1);
}
const last = isbn[9];
if (last === "X") {
sum += 10 * 10;
} else {
const digit = Number(last);
if (Number.isNaN(digit)) return false;
sum += digit * 10;
}
return sum % 11 === 0;
}