Skip to content

Instantly share code, notes, and snippets.

@littleomie
Created August 31, 2023 07:38
Show Gist options
  • Save littleomie/181e31cd76c43893b822d59597b23079 to your computer and use it in GitHub Desktop.
Save littleomie/181e31cd76c43893b822d59597b23079 to your computer and use it in GitHub Desktop.
Container Code Check Digit Calculation - ISO 6346
// let input = 'ECMU465749'; // 6
let input = 'GVTU300038'; // 9
function getCheckDigit(input) {
const char_map = {
'A': 10, 'B': 12, 'C': 13, 'D': 14, 'E': 15, 'F': 16, 'G': 17,
'H': 18, 'I': 19, 'J': 20, 'K': 21, 'L': 23, 'M': 24, 'N': 25,
'O': 26, 'P': 27, 'Q': 28, 'R': 29, 'S': 30, 'T': 31, 'U': 32,
'V': 34, 'W': 35, 'X': 36, 'Y': 37, 'Z': 38
};
// split per character
let parts = input.split('');
// replace first for letter value with number
parts[0] = char_map[parts[0]];
parts[1] = char_map[parts[1]];
parts[2] = char_map[parts[2]];
parts[3] = char_map[parts[3]];
// convert all to numbers
parts = parts.map(x => parseInt(x, 10));
// is multiplied by 2position, where position is the exponent to basis 2
parts = parts.map((x, i) => x * Math.pow(2, i));
// sum all
const a = parts.reduce(function (a, b) {
return a + b;
}, 0);
// divide by 11
const b = a / 11;
// remove decimal part
const c = Math.floor(b);
// multiply by 11
const d = c * 11;
// subtract from a
return a - d;
}
let check_digit = getCheckDigit(input);
console.log(input, check_digit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment