4

I have JavaScript array with numbers 0 and 1 and I need to make a sum of all numbers in same row and column (if I imagine my array in two dimensions). I want to create second array with sums for every single value in first array.

2D array visualization for first item (X-Y table of values):
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
(Sum: 8 for value at index 0;0 (with value itself excluded))

Real array I have:
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
(Sum: 8 for value at index 0)

2D array visualization for second item:
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1
1,1,1,1,1

(Sum: 8 for value at index 1;0)
This way I need to loop through whole array.

2D array visualization for second item with zero separator:
1,1,1,1,1
1,1,1,1,1
1,0,1,1,1
1,1,1,1,1
1,1,1,1,1
(Sum: 5 for value at index 1;0)
Values after zero I don't want to count in.

For previous table the result table should be like...
8,5,8,8,8
8,5,8,8,8
4,x,6,6,6
8,5,8,8,8
8,5,8,8,8

Thank you very much for your help!

5
  • I'm not exactly sure what the question here is. If the question is "is this possible", then yes, it definitely is. Commented Jul 8, 2015 at 13:47
  • 1
    This is going to look ugly and be difficult to read later when you need to maintain it. I'd strongly recommend converting it into a multidimensional array first, and then simple loops can do the calculations for you. Commented Jul 8, 2015 at 13:47
  • 1
    Convert to multidimensional array first looks like the best approach. Commented Jul 8, 2015 at 13:48
  • Of course it is possible, but I don't know how... I'am pretty confused with multidimensional arrays in JS. I used to work with them in C#, but in JS it doesn't work as I expected. Commented Jul 8, 2015 at 13:50
  • OK, I have 2D array now. Array 0 [ 1, 1, 0, 1 ] Array 1 [ 1, 1, 1, 1 ] Array 2 [ 1, 1, 1, 1 ] Array 3 [ 1, 1, 0, 1 ] But I can't figure out how to sum the value I want. Commented Jul 10, 2015 at 10:25

1 Answer 1

2
var res = [];  //the 1D array to hold the sums
var hArr =  [
   [ 1, 1, 1, 1 ],
   [ 1, 1, 1, 1 ],
   [ 1, 0, 0, 1 ],
   [ 1, 1, 0, 0 ]
]; //your array


var vArr = []; //Now lets create an array of arrays with the columns of hArr

for (var j=0; j<hArr[0].length; j++) {
  var temp = [];
  for (var i=0; i<hArr.length; i++) {
      temp.push(hArr[i][j]);
  }
  vArr.push(temp);
}

//sum all the element in the line - Vertically and Horizontally
function SumVH (hInd, vInd) {
  var sum = 0;
  //add horizontal elements
  //to the left
  for(var i=(vInd-1); i>=0; i--) {
    //if a 0 is found, break
    if (hArr[hInd][i] == 0) {
      break;
    }
    sum += hArr[hInd][i];
  }

  //to the right
  for(var i=(vInd+1); i<hArr[hInd].length; i++) {
    //if a 0 is found, break
    if (hArr[hInd][i] == 0) {
      break;
    }
    sum += hArr[hInd][i];
  }

  //add vertical elements
  //towards top
  for(var i=(hInd-1); i>=0; i--) {
    //if a 0 is found, break
    if(vArr[vInd][i] == 0) {
      break;
    }
    sum += vArr[vInd][i];
  }

  //towards bottom
  for(var i=(hInd+1); i<vArr[vInd].length; i++) {
    //if a 0 is found, break
    if(vArr[vInd][i] == 0) {
      break;
    }
    sum += vArr[vInd][i];
  }  
  //console.log("hInd="+hInd+" vInd="+vInd+" Sum="+sum);
  return sum;
}

// go through the main array and get result
var sumR = 0;
//sum of each row
for (var i=0; i<hArr.length; i++) {
   for (var j=0; j<hArr[i].length; j++) {    
      sumR = SumVH(i,j);
      res.push(sumR);
   }   
}

Please check it and let me know if it is working as you expect it to work. res variable holds the result.

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

13 Comments

I have edited my answer to stop adding if a zero is encountered in the path. Please check and let me know if it satisfies your requirement. Thank you :) @Jan Chalupa
This is really going to be working great, but there is some problem. In most cases it gives mi values I expect but sometimes don't. Array [ 1, 1, 1, 1 ] Array [ 1, 0, 0, 0 ] Array [ 1, 0, 1, 1 ] Array [ 1, 1, 0, 1 ] Array [ 6, 3, 3, 3, 3, 2, 2, 2, 3, 2, 0, 0, 4, 1, 3, 1 ] The second zero is wrong.
Found the bug and fixed it. I think it should not be giving you trouble anymore :) I have also updated it with your new test array from previous comment. And now the value of "res" is [6, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 2, 4, 2, 3, 3] Please check and confirm if it is working as expected. @JanChalupa
Unfortunately is it the same. In most cases it works, but for example this case: [ 1, 1, 1, 1 ], [ 1, 1, 1, 1 ], [ 1, 0, 0, 1 ], [ 1, 1, 0, 0 ] it doesn't. It gives me result: [ 6, 4, 4, 5, 6, 4, 4, 5, 3, 3, 3, **3**, 4, **3**, 4, 5 ] I don't know why it sometimes work and sometimes don't. But I think that every error values are in the right bottom corner - is that usefull information? But in all cases your the king! :)
I'm sorry but my English isn't the best one - now I'm little bit lost of what are you tellin' me. :D I'll try to describe it by an image. First two horizontal lines are perfect (thank you for that), but the last two lines are wrong. Number should be red,red,3,2 for last but one line and red,0,red,1 for the last one. Red dots values doesn't matter to me. The dots should contain number of others blue dots near to them.
|

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.