Skip to content

Instantly share code, notes, and snippets.

@byigitt
Created December 1, 2023 08:08
Show Gist options
  • Select an option

  • Save byigitt/5ab00398e6b220555609afc11985772c to your computer and use it in GitHub Desktop.

Select an option

Save byigitt/5ab00398e6b220555609afc11985772c to your computer and use it in GitHub Desktop.
creating & checking turkish identity numbers
function randomInteger (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
function idGenerator () {
let n = [];
for (let i = 0; i < 9; i++) {
n.push(randomInteger(1, 9));
};
n.push(((n[0]+n[2]+n[4]+n[6]+n[8]) * 7 - n[1]+n[3]+n[5]+n[7]) % 10); // 10th element
n.push(n.reduce((a, b) => a + b, 0) % 10) // 11th element
return n.join("");
};
function idChecker (id) {
let n = typeof id !== Number ? id.split("").map(i=>Number(i)) : String(id).split("").map(i=>Number(i));
return (((n[0]+n[2]+n[4]+n[6]+n[8]) * 7 - n[1]+n[3]+n[5]+n[7]) % 10 == n[9] && n.reduce((a, b, i) => i < 10 ? a + b : a, 0) % 10 == n[10]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment