我是一名正在家中学习的学生,我严重困在Checksum问题上。该脚本应该验证PESEL(波兰语等同于我认为的社会安全号码),无论如何,校验和的工作原理如下: PESEL:70051012347
PESEL:7,0,0,5,1,0,1,2,3,4(7)
racket/base
CHECK:1,3,7,9,1,3,7,9,1,3
(Multiply each Pesel number by its corresponding check number)
SUM:+ 7,0,0,45,1,0,7,18,3,12 = 93
MOD:93 MOD 10 = 3
10 - 3 = 7(pesel的最后一位数)
如果MOD 10不等于0,则从10中减去总和%10的结果,然后与原始数字中的最后一位匹配,如果它们匹配其好处,如果不是坏的话。我需要的只是一个好的或坏的结果。
我很确定我的代码中包含了所有这些内容,并且有一个简单的解决方案我无法看到它。任何帮助都会受到大力赞赏。
(Sum Each number)
答案 0 :(得分:1)
You have made a couple of mistakes. If you step through your code using a JavaScript debugger, you will find out exactly what goes wrong. The most important fact is, that you don't have to convert a string to an array of integers. JavaScript automatically understands when to convert a character to an integer. This is my solution:
function peselgood(y)
{
if (y.length == 11)
{
var arr = [1,3,7,9,1,3,7,9,1,3];
var sum = 0;
//muliply pesel digits by checksum digits
for (var i = 0; i < 10; i++)
{
sum += arr[i] * y[i];
}
sum = sum%10 == 0 ? 0 : 10-sum%10;
return sum == y[10];
}
else
{
return false;
}
}