1

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

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0
  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/

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.