---
id: 69b1028d6e265413d0198a2a
title: "Challenge 231: ISBN-10 Validator"
challengeType: 28
dashedName: 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`.

```js
assert.isTrue(isValidIsbn10("0-306-40615-2"));
```

`isValidIsbn10("0-306-40615-1")` should return `false`.

```js
assert.isFalse(isValidIsbn10("0-306-40615-1"));
```

`isValidIsbn10("0-8044-2957-X")` should return `true`.

```js
assert.isTrue(isValidIsbn10("0-8044-2957-X"));
```

`isValidIsbn10("X-306-40615-2")` should return `false`.

```js
assert.isFalse(isValidIsbn10("X-306-40615-2"));
```

`isValidIsbn10("0-6822-2589-4")` should return `true`.

```js
assert.isTrue(isValidIsbn10("0-6822-2589-4"));
```

# --seed--

## --seed-contents--

```js
function isValidIsbn10(str) {

  return str;
}
```

# --solutions--

```js
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;
}
```
