Associate
I am trying to determine what algorithm or method has been used to generate a check digit for a set of data I have.
The data is the 5-Digit price value of a 5-Digit random weight EAN-13 barcode. (i think that's what there called)
I've tried a few different algorithms so far to no avail.
Anyone had experience with check digits and the like and can name a few that I can try?
Below is my less than wonderful testing thing along with the data just in case you want a laugh :/
The data is the 5-Digit price value of a 5-Digit random weight EAN-13 barcode. (i think that's what there called)
I've tried a few different algorithms so far to no avail.
Anyone had experience with check digits and the like and can name a few that I can try?
Below is my less than wonderful testing thing along with the data just in case you want a laugh :/
Code:
var set = [
{ "data": [0,0,4,2,0], "check": 9 },
{ "data": [0,1,3,2,0], "check": 5 },
{ "data": [0,0,2,2,5], "check": 9 },
{ "data": [0,0,2,2,5], "check": 9 },
{ "data": [0,0,1,5,0], "check": 4 },
{ "data": [0,0,3,8,0], "check": 7 },
{ "data": [0,0,2,2,5], "check": 9 },
{ "data": [0,0,3,6,3], "check": 6 },
{ "data": [0,0,6,3,0], "check": 6 },
{ "data": [0,1,6,1,0], "check": 8 },
{ "data": [0,0,2,2,5], "check": 9 },
{ "data": [0,1,6,5,0], "check": 3 },
{ "data": [0,0,3,2,8], "check": 0 },
{ "data": [0,0,2,2,5], "check": 9 },
{ "data": [0,1,6,8,0], "check": 6 },
{ "data": [0,2,9,0,0], "check": 1 },
{ "data": [0,0,2,2,5], "check": 9 },
{ "data": [0,0,4,8,0], "check": 4 },
{ "data": [0,0,9,2,0], "check": 4 },
{ "data": [0,0,3,2,5], "check": 8 },
{ "data": [0,2,1,6,3], "check": 7 },
{ "data": [0,1,6,8,0], "check": 6 },
{ "data": [0,3,4,8,0], "check": 7 }
];
var algorithm = [
{
execute: function(data) {
var sum = 0
for (var i=0, l=data.length; i<l; i++) {
if (i % 2 === 0) {
sum += data[i]*3;
} else {
sum += data[i];
}
}
if (sum % 2 === 0) {
sum -= Math.floor(sum/10)*10;
} else {
sum -= (Math.floor(sum/10)+1)*10;
}
return sum;
},
name: "ean-13"
},
{
execute: function(data) {
for (var i=0, l=data.length; i<l; i++) {
var cd = 0;
if (i % 2 === 0) {
if (data[i]*2>10) {
cd += (data[i]*2)[0]+(data[i]*2)[1];
} else {
cd += data[i]*2;
}
} else {
if (data[i]>10) {
cd += (data[i])[0]+(data[i])[1];
} else {
cd += data[i];
}
}
}
return cd;
},
name: "luhn"
},
{
execute: function(data) {
var wf2m = [0,2,4,6,8,9,1,3,5,7];
var wf5p = [0,5,1,6,2,7,3,8,4,9];
var wf5m = [0,5,9,4,8,3,7,2,6,1];
var wf = [wf5p,wf2m,wf5m,wf5p,wf2m];
var sum = 0;
for (var i=0, l=data.length; i<l; i++) {
sum += wf[i][data[i]];
}
if (Math.floor(sum/10) === (sum/10)) {
sum = Math.floor(sum/10)*10 - sum;
} else {
sum = (Math.floor(sum/10)+1)*10 - sum;
}
return wf5m[Math.abs(sum)+1];
},
name: "gs1tw_5digit",
note: "http://www.gs1tw.org/twct/web/BarCode/GS1_Section1V6-0_all.pdf"
}
];
for (var a=algorithm.length; a--;) {
var p = 0, t = 0;
console.log("Testing [" + algorithm[a].name + "]");
for (var i=set.length; i--;) {
if (Math.abs(algorithm[a].execute(set[i].data)) === set[i].check) {
p++;
}
t++;
}
console.log("Test Complete [" + p + "] of [" + t + "] Passed");
}
Last edited: