Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate sum of multiple responses inside a loop using javascript?

I have two loops as shown:

        for (var i = 0; i < nearPlacements.length; i++) {
           var meshInstances = nearPlacements[i].model.model.meshInstances;
var mat;
var matEach = 0;
for (var i2 = 0; i2 < meshInstances.length; i2++) {
    mat = parseFloat(meshInstances[i2].mesh.indexBuffer[0].numIndices / 3);
    matEach += mat;
    }
    console.log(matEach);
}

console.log(matEach); outputs data as:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How to display data as a sum of all responses? Thanks

>Solution :

its like what you did with matEach variable but outside outer loop.

let total = 0;
for (var i = 0; i < nearPlacements.length; i++) {
  var meshInstances = nearPlacements[i].model.model.meshInstances;
  var mat;
  var matEach = 0;
  for (var i2 = 0; i2 < meshInstances.length; i2++) {
    mat = parseFloat(meshInstances[i2].mesh.indexBuffer[0].numIndices / 3);
    matEach += mat;
  }
  console.log(matEach);
  total += matEach;
}

console.log(total);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading