Javascript如何对整数进行四舍五入并找到其加法?
目标 我正处于编写Luhn算法脚本的最后阶段 问题 假设我有一个Javascript如何对整数进行四舍五入并找到其加法?,javascript,luhn,Javascript,Luhn,目标 我正处于编写Luhn算法脚本的最后阶段 问题 假设我有一个73 我如何将其四舍五入到下一个0?因此,最终值是80 最后,我怎样才能得到进行加法的值呢?e、 g.7是最终答案 当前代码 function validateCred(array) { // Array to return the result of the algorithm const algorithmValue = []; // Create a [2, 1, 2] Pattern const patte
73
我如何将其四舍五入到下一个0
?因此,最终值是80
最后,我怎样才能得到进行加法的值呢?e、 g.7
是最终答案
当前代码
function validateCred(array) {
// Array to return the result of the algorithm
const algorithmValue = [];
// Create a [2, 1, 2] Pattern
const pattern = array.map((x, y) => {
return 2 - (y % 2);
});
// From given array, multiply each element by it's pattern
const multiplyByPattern = array.map((n, i) => {
return n * pattern[i];
});
// From the new array, split the numbers with length of 2 e.g. 12 and add them together e.g. 1 + 2 = 3
multiplyByPattern.forEach(el => {
// Check for lenght of 2
if(el.toString().length == 2) {
// Split the number
const splitNum = el.toString().split('');
// Add the 2 numbers together
const addSplitNum = splitNum.map(Number).reduce(add, 0);
// Function to add number together
function add(accumalator, a) {
return accumalator + a;
}
algorithmValue.push(addSplitNum);
}
// Check for lenght of 1
else if(el.toString().length == 1){
algorithmValue.push(el);
}
});
// Sum up the algorithmValue together
const additionOfAlgorithmValue = algorithmValue.reduce((a, b) => {
return a + b;
});
// Mod the final value by 10
if((additionOfAlgorithmValue % 10) == 0) {
return true;
}
else{
return false;
}
}
// Output is False
console.log(validateCred([2,7,6,9,1,4,8,3,0,4,0,5,9,9,8]));
上述代码摘要
function validateCred(array) {
// Array to return the result of the algorithm
const algorithmValue = [];
// Create a [2, 1, 2] Pattern
const pattern = array.map((x, y) => {
return 2 - (y % 2);
});
// From given array, multiply each element by it's pattern
const multiplyByPattern = array.map((n, i) => {
return n * pattern[i];
});
// From the new array, split the numbers with length of 2 e.g. 12 and add them together e.g. 1 + 2 = 3
multiplyByPattern.forEach(el => {
// Check for lenght of 2
if(el.toString().length == 2) {
// Split the number
const splitNum = el.toString().split('');
// Add the 2 numbers together
const addSplitNum = splitNum.map(Number).reduce(add, 0);
// Function to add number together
function add(accumalator, a) {
return accumalator + a;
}
algorithmValue.push(addSplitNum);
}
// Check for lenght of 1
else if(el.toString().length == 1){
algorithmValue.push(el);
}
});
// Sum up the algorithmValue together
const additionOfAlgorithmValue = algorithmValue.reduce((a, b) => {
return a + b;
});
// Mod the final value by 10
if((additionOfAlgorithmValue % 10) == 0) {
return true;
}
else{
return false;
}
}
// Output is False
console.log(validateCred([2,7,6,9,1,4,8,3,0,4,0,5,9,9,8]));
输出应为True
。这是因为,我已经给出了数组中15位的总长度。而它应该是16。我知道第16个值是7
,因为给定数组的总值是73,将其四舍五入到下一个0是80,这意味着校验位是7
问题
如果给定的数组长度小于15,如何获取校验号?我想您需要的是:
var oldNum=73
var newNum=Math.ceil((oldNum+1)/10)*10;;
然后使用以下方法检查差异:
Math.abs(newNum-oldNum);
我想您需要的是:
var oldNum=73
var newNum=Math.ceil((oldNum+1)/10)*10;;
然后使用以下方法检查差异:
Math.abs(newNum-oldNum);
您可以这样做:
设x=[73,81,92101423];
设y=x.map((v)=>{
设余数=v%10;
设nextrondend=v+(10余数);
/*或者你可以用
设nextRounded=(parseInt(v/10)+1)*10;
*/
设amountToNextRounded=10-余数;
返回[nextrandound,amounttonextrandound];
});
控制台日志(y)代码>您可以这样做:
设x=[73,81,92101423];
设y=x.map((v)=>{
设余数=v%10;
设nextrondend=v+(10余数);
/*或者你可以用
设nextRounded=(parseInt(v/10)+1)*10;
*/
设amountToNextRounded=10-余数;
返回[nextrandound,amounttonextrandound];
});
控制台日志(y)代码>如果你用10修改73,你会得到一个余数。。。。80不。。。我认为这是一个开始,也是你应该继续的整个想法。是的,你是对的。但是如果我在数组的末尾添加了一个7,那么将得到80。但我现在的难题是,如何在代码中计算出7。因为数组的总长度应等于16(包括校验位)。如果给定15,我需要找到校验位。使用模数@PeterDarmis said10-(73%10)=7
@pilchard,这很有意义!非常感谢。如果你用10模73,你会得到一个余数。。。。80不。。。我认为这是一个开始,也是你应该继续的整个想法。是的,你是对的。但是如果我在数组的末尾添加了一个7,那么将得到80。但我现在的难题是,如何在代码中计算出7。因为数组的总长度应等于16(包括校验位)。如果给定15,我需要找到校验位。使用模数@PeterDarmis said10-(73%10)=7
@pilchard,这很有意义!谢谢大家!nextrondend
只是v+(10个余数)
@pilchard是的,找到nextrondend的一种方法是你写的,我认为它更简单。nextrondend
只是v+(10个余数)
@pilchard是的,找到nextrondend的一种方法是你写的,我认为它更简单。