Skip to content

Instantly share code, notes, and snippets.

View Wyzix33's full-sized avatar

Gabriel Wyzix33

  • Romania
View GitHub Profile
@Atinux
Atinux / last-digit-ean13.js
Last active November 29, 2019 18:07
Get last digit of an EAN/GTIN 13 in JavaScript
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]
})