Skip to content

Instantly share code, notes, and snippets.

@sadikay
Forked from defiant/verifyVkn.js
Last active November 8, 2023 13:24
Show Gist options
  • Save sadikay/7847f15100efbdaf036fbec937639857 to your computer and use it in GitHub Desktop.
Save sadikay/7847f15100efbdaf036fbec937639857 to your computer and use it in GitHub Desktop.
Vergi Kimlik Numarası (VKN) doğrulama.
// Examples:
// Valid: "3973535717", "2037637860", "2823097943", "2012460224"
// Not Valid: "3973535711", "123", "9999999999",
function verifyVkn(vkn) {
if (typeof vkn !== 'string') throw new TypeError('vkn should be a string');
if (vkn.length !== 10) throw new TypeError('invalid length');
const digits = vkn.split('');
const control = digits.pop(); // eslint-disable-line
const r = [];
digits.forEach((el, i) => {
const x = Number(el) + 10 - (i + 1);
const mod = x % 10;
if (mod === 9) {
r.push(mod);
} else {
const pow = 2 ** (10 - (i + 1));
r.push((mod * pow) % 9);
}
});
const total = r.reduce((acc, cur) => acc + cur);
const c = (10 - ((total % 10) % 10)) % 10;
return `${digits.join('')}${c}` === vkn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment