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.
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>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.