5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptで13桁のISBNを10桁に変換する

Posted at

前回書いたJavaScriptで10桁のISBNを13桁に変換するの逆バージョン。
13桁も10桁にすることが可能。

const toISBN10 = (isbn13) => {
  // 1. 先頭3文字と末尾1文字を除く
  const src = isbn13.slice(3, 12);

  // 2. 先頭の桁から順に10、9、8…2を掛けて合計する
  const sum = src.split('').map(s => parseInt(s))
    .reduce((p, c, i) => (i === 1 ? p * 10 : p) + c * (10 - i));

  // 3. 合計を11で割った余りを11から引く(※引き算の結果が11の場合は0、10の時はアルファベットのXにする)
  const rem = 11 - sum % 11;
  const checkdigit = rem === 11 ? 0 : (rem === 10 ? 'X' : rem);

  // 1.の末尾に3.の値を添えて出来上がり
  return `${src}${checkdigit}`;
};
5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?