Skip to content

Instantly share code, notes, and snippets.

@furudean
Last active June 26, 2024 16:03
Show Gist options
  • Select an option

  • Save furudean/3dd4bf99241ff54e6926fc4abe3a3215 to your computer and use it in GitHub Desktop.

Select an option

Save furudean/3dd4bf99241ff54e6926fc4abe3a3215 to your computer and use it in GitHub Desktop.
Generate EAN13 check digit
/**
* Generates a check digit from a partial EAN13.
*
* https://www.gs1.org/services/how-calculate-check-digit-manually
*
* @param {string} barcode - 12 digit EAN13 barcode without the check digit
*/
function checkDigitEAN13(barcode) {
const sum = barcode.split('')
.map((n, i) => n * (i % 2 ? 3 : 1)) // alternate between multiplying with 3 and 1
.reduce((sum, n) => sum + n, 0) // sum all values
const roundedUp = Math.ceil(sum / 10) * 10; // round sum to nearest 10
const checkDigit = roundedUp - sum; // subtract round to sum = check digit
return checkDigit;
}
@luiscvz
Copy link

luiscvz commented Sep 29, 2021

Thanks for your job! :)

@KassioSantos
Copy link

Thanks

@1x6
Copy link

1x6 commented Jun 26, 2024

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment