【发布时间】:2018-05-03 02:53:16
【问题描述】:
enter image description here我有一个这样的数字列表:
"2552086989552589"
"6724843711060148"
"9758289300869651"
"5048166833276726"
"2864448008247645"
"6803999652011971"
"9758289300869650"
"9083938527182086"
"4563447869509114"
"2552086989552588"
"6724843711060149"
"6803999652011970"
"4563447869509115"
"3227366834142384"
"3227366834142385"
"5048166833276727"
"5153186889177226"
"2864448008247644"
"9083938527182087"
它们是这个函数的输出
function validate (ban,cards){
return cards;
}
我怎样才能得到卡片的输出并进行计算,例如“123123123”中数字的两倍之和,这些也是当前的字符串,所以需要先转换为int!
目标是
{"card":"2552086989552589","isValid":false,"isAllowed":true}
{"card":"6724843711060148","isValid":true,"isAllowed":true}
{"card":"9758289300869651","isValid":false,"isAllowed":false}
{"card":"5048166833276726","isValid":true,"isAllowed":false}
{"card":"2864448008247645","isValid":false,"isAllowed":true}
{"card":"6803999652011971","isValid":false,"isAllowed":true}
{"card":"9758289300869650","isValid":true,"isAllowed":false}
{"card":"9083938527182086","isValid":true,"isAllowed":false}
{"card":"4563447869509114","isValid":true,"isAllowed":true}
{"card":"2552086989552588","isValid":true,"isAllowed":true}
{"card":"6724843711060149","isValid":false,"isAllowed":true}
{"card":"6803999652011970","isValid":true,"isAllowed":true}
{"card":"4563447869509115","isValid":false,"isAllowed":true}
{"card":"3227366834142384","isValid":true,"isAllowed":true}
{"card":"3227366834142385","isValid":false,"isAllowed":true}
{"card":"5048166833276727","isValid":false,"isAllowed":false}
有效是取前 15 个数字,将它们加倍,求和,然后除以 10。如果余数是最后一个数字,则 isvalid 为真,isallowed 为真。
更新
这非常接近工作:
function validateCards(bannedPrefixes, cardsToValidate) {
return cardsToValidate.map(function(card) {
const digits = card.split ('').map(Number);
const valid = digits.reduce ((acc,d)=>acc+d*2, 0)%10== digits[digits.length-1]
return {
card: card,
isValid: valid,
isAllowed: valid,
}
})
}
我只需要看看cardstovalidate是否包含禁止前缀,是否需要返回false
【问题讨论】:
-
成功了!现在我怎样才能对其中的每一个做一个数学函数!
-
如果我想打印原始数字,如 "original":"123123" 和右边的打印 "sum": "4553" 我该怎么做
-
a+b 抛出一些错误
-
要打印2*卡号吗?
-
我要打印 {"card":"2552086989552589","isValid":false,"isAllowed":true}
标签: javascript json data-manipulation