[英]how to multiply and adding two dimensional arrays
I have variable with string values in two dimensional array format. 我有二维数组格式的字符串值变量。
var arrayList=[["1","2"],["6","3600","11","60"],["1","2","3","4","5","6"]];
What I want,each odd position value multiply with next even position and finally adding that values 我想要的是,每个奇数位置值乘以下一个偶数位置并最终添加该值
like. 喜欢。
["1","2"]=(1*2);
["6","3600","11","60"]=((6*3600)+(11*60));
["1","2","3","4","5","6"]=((1*2)+(3*4)+(5*6))
for this I written the following code,second and third cases are not working. 为此我写了下面的代码,第二和第三种情况都不起作用。
really sorry might be it's very basic question but I tested each and every line it's seems code is correct but in second and third cases getting Nan
. 真的很遗憾可能是这是一个非常基本的问题,但我测试了每一行它似乎代码是正确的但在第二和第三种情况下得到了Nan
。
var result=[];
for (var index = 0; index < arrayList.length; index++) {
var innerResult=0;
for (var jndex = 0; jndex < arrayList[index].length; jndex++) {
var cali=parseInt(arrayList[index][jndex])*parseInt(arrayList[index][jndex+1]);
innerResult=innerResult+cali;
jndex=jndex+2;
};
result.push(innerResult);
};
result
I am getting like this [3,Nan,Nan]
. 我变得像这样[3,Nan,Nan]
。
please can anyone help me. 请任何人都可以帮助我。
Thanks 谢谢
You're incrementing jndex
on each loop and then you are adding 2 more at the end of that loop. 您在每个循环上递增jndex
,然后在该循环结束时再添加2个。 You have two options, changing this: 你有两个选择,改变这个:
for (var jndex = 0; jndex < arrayList[index].length; jndex++) {
to: 至:
for (var jndex = 0; jndex < arrayList[index].length; jndex+=2 ) {
or this: 或这个:
jndex=jndex+2;
to: 至:
jndex=jndex+1;
If you do the first one, you no longer need the increment within the loop. 如果您执行第一个,则不再需要循环内的增量。
I have written this algorithm that I believe might help you. 我写过这个算法,我相信可能对你有帮助。
var array = [["1","2"],["6","3600","11","60"],["1","2","3","4","5","6"]];
array.map(function(subArray){
var total = 0;
for(var i = 1; i < subArray.length; i += 2)
total += parseInt(subArray[i], 10) * parseInt(subArray[i - 1], 10);
return total;
});
The jindex
will be incremented by the loop as well. 循环也会增加jindex
。 This will mean the jindex
is incremented by 3 each loop. 这意味着jindex
每个循环增加3。
Consider the case where jindex
is arrayList[index].length - 1
; 考虑其中的情况下jindex
是arrayList[index].length - 1
; when you parseInt(arrayList[index][jndex+1])
you will reach outside the bounds of the array, and get undefined
(and parseInt(undefined)
is NaN
again). 当你parseInt(arrayList[index][jndex+1])
你将到达数组的边界之外,并获得undefined
(并且parseInt(undefined)
再次是NaN
)。
If you fix those, you should find your code works; 如果你修复它们,你应该发现你的代码有效;
var result = [];
for (var index = 0; index < arrayList.length; index++) {
var innerResult = 0;
for (var jndex = 0; jndex < arrayList[index].length - 1; jndex++) {
var cali = parseInt(arrayList[index][jndex]) * parseInt(arrayList[index][jndex + 1]);
innerResult = innerResult + cali;
jndex = jndex + 1;
};
}
Try this: 尝试这个:
var result = [];
for (var index = 0; index < arrayList.length; index++) {
var innerResult = 0;
for (var jndex = 0; jndex < arrayList[index].length;) {
var cali = (parseInt(arrayList[index][jndex]) * parseInt(arrayList[index][jndex + 1]));
innerResult = innerResult + cali;
jndex = jndex + 2;
}
result.push(innerResult);
}
Inner for loop changed to while loop (you have double increment in for loop): Inner for循环更改为while循环(for循环中有双倍增量):
var result = [];
for (var index = 0; index < arrayList.length; index++) {
var innerResult = 0;
var j = 0;
while (j < arrayList[index].length) {
var cali = parseInt(arrayList[index][j]) * parseInt(arrayList[index][j + 1]);
innerResult = innerResult + cali;
j += 2;
}
result.push(innerResult);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.