简体   繁体   中英

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 .

 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] .

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. 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; 
});
  1. The jindex will be incremented by the loop as well. This will mean the jindex is incremented by 3 each loop.

  2. Consider the case where jindex is 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).

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;
      };
  }

http://jsfiddle.net/Bwx2g/

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):

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);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM