Last active
October 30, 2017 11:57
-
-
Save albertodeste/48eba5592f274de202a380a69a3b4f62 to your computer and use it in GitHub Desktop.
Script for creating an EAN13 code with correct check digit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverse(s) | |
{ | |
splitext = s.split(""); | |
revertext = splitext.reverse(); | |
reversed = revertext.join(""); | |
return reversed; | |
} | |
function eanCheckDigit(s) | |
{ | |
var result = 0; | |
var rs = reverse(s); | |
for (counter = 0; counter < rs.length; counter++) | |
{ | |
result = result + parseInt(rs.charAt(counter)) * Math.pow(3, ((counter+1) % 2)); | |
} | |
return (10 - (result % 10)) % 10; | |
} | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
function generateEan13WithoutCheckDigit() { | |
var eanCode = ""; | |
for(var i=0; i<12; i++){ | |
eanCode += getRandomInt(0, 9); | |
} | |
return eanCode; | |
} | |
var eanCode = generateEan13WithoutCheckDigit(); | |
prompt("EAN13", eanCode + eanCheckDigit(eanCode)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function(){for(var eanCode="",i=0;12>i;i++)eanCode+=Math.floor(10*Math.random());var checkDigit=0,rs=eanCode.split("");for(rs=rs.reverse(),rs=rs.join(""),counter=0;counter<rs.length;counter++)checkDigit+=parseInt(rs.charAt(counter))*Math.pow(3,(counter+1)%2);checkDigit=(10-checkDigit% 10)% 10,prompt("EAN13",eanCode+checkDigit);})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment