Skip to content

Instantly share code, notes, and snippets.

@shauns
Created September 11, 2020 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shauns/fa7a17c7bf6390ba6fd75b63f439fc17 to your computer and use it in GitHub Desktop.
Save shauns/fa7a17c7bf6390ba6fd75b63f439fc17 to your computer and use it in GitHub Desktop.
Damm Algorithm check digit for Google Sheets
// See https://en.wikipedia.org/wiki/Damm_algorithm
// =DAMM(5724) == true
const cols = {
"0": {
"0": "0",
"1": "7",
"2": "4",
"3": "1",
"4": "6",
"5": "3",
"6": "5",
"7": "8",
"8": "9",
"9": "2"
},
"1": {
"0": "3",
"1": "0",
"2": "2",
"3": "7",
"4": "1",
"5": "6",
"6": "8",
"7": "9",
"8": "4",
"9": "5"
},
"2": {
"0": "1",
"1": "9",
"2": "0",
"3": "5",
"4": "2",
"5": "7",
"6": "6",
"7": "4",
"8": "3",
"9": "8"
},
"3": {
"0": "7",
"1": "2",
"2": "6",
"3": "0",
"4": "3",
"5": "4",
"6": "9",
"7": "5",
"8": "8",
"9": "1"
},
"4": {
"0": "5",
"1": "1",
"2": "8",
"3": "9",
"4": "0",
"5": "2",
"6": "7",
"7": "3",
"8": "6",
"9": "4"
},
"5": {
"0": "9",
"1": "5",
"2": "7",
"3": "8",
"4": "4",
"5": "0",
"6": "2",
"7": "6",
"8": "1",
"9": "3"
},
"6": {
"0": "8",
"1": "4",
"2": "1",
"3": "3",
"4": "5",
"5": "9",
"6": "0",
"7": "2",
"8": "7",
"9": "6"
},
"7": {
"0": "6",
"1": "8",
"2": "3",
"3": "4",
"4": "9",
"5": "5",
"6": "1",
"7": "0",
"8": "2",
"9": "7"
},
"8": {
"0": "4",
"1": "6",
"2": "5",
"3": "2",
"4": "7",
"5": "8",
"6": "3",
"7": "1",
"8": "0",
"9": "9"
},
"9": {
"0": "2",
"1": "3",
"2": "9",
"3": "6",
"4": "8",
"5": "1",
"6": "4",
"7": "7",
"8": "5",
"9": "0"
}
}
function DAMM(input) {
const allDigits = input.toString().split('');
const digits = allDigits.splice(0, allDigits.length - 1);
const check = allDigits[allDigits.length - 1];
let rowIndex = "0"
for (let colIndex of digits) {
rowIndex = cols[colIndex][rowIndex]
}
return rowIndex === check;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment