EAN13 (barcode) check digit user macro

Reto Eggenberger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2018

Hi

I want to make a simple user macro, which calculates the ean13 check digit with javascript. But I'm absolutely not used to velocity. Maybe someone can give me a jump start ...

I've found this javascript code to calculate the 13th digit:

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

How can I display this script result on the page?

I guess I have to wrap it in a script tag and I tried this:

getLastEan13Digit('$paramEAN13');

<script type="text/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];
})
const base10Superior = Math.ceil(total / 10) * 10;
return = base10Superior - total;
}
</script>

But of course this doesn't execute the script.

Thx for any hints. 

1 answer

1 accepted

0 votes
Answer accepted
Reto Eggenberger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2018

Got it.

Just use normal jQuery syntax ...

<div id="result">Test</div>

<script type="text/javascript">
jQuery(document).ready(function () {
jQuery('#result').text('$paramEAN13'+getLastEan13Digit('$paramEAN13'));
});

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;
};
</script>

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events