// 身份证校验标准 ISO 7064:1983.MOD 11-2
function check(id) {
let str = id.toString(); // 初始化, 转换为字符串
// 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2
// 乘数
let coef = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
let sum = 0;
for (i = 0; i < 17; i++) {
sum += str[i] * coef[i];
}
let remainder = sum % 11;
/*
余数和身份证最后一位的对应规则
0-1-2-3-4-5-6-7-8-9-10
1-0-X-9-8-7-6-5-4-3-2
*/
let validation = new Map([
[0, "1"],
[1, "0"],
[2, "X"],
[3, "9"],
[4, "8"],
[5, "7"],
[6, "6"],
[7, "5"],
[8, "4"],
[9, "3"],
[10, "2"]
])
return validation.get(remainder) == str[17];
}
let sample_real = 410901199704024012n;
let sample_fake = 410901199704024013n;
console.log(check(sample_fake));
console