📜  javascript ean13 校验和生成 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:43.194000             🧑  作者: Mango

代码示例1
function getLastEan13Digit(ean) { 
  if (!ean || ean.length !== 12) throw new Error('Invalid EAN 13, should have 12 digits'); 
  const multiply = [1, 3]; 
  let total = 0; 
  ean.split('').forEach((letter, index) => { 
    total += parseInt(letter, 10) * multiply[index % 2]; 
  }); 
  const base10Superior = Math.ceil(total / 10) * 10; 
  return base10Superior - total;
}