Home > Blockchain >  How to calculate sum of multiple responses in javascript?
How to calculate sum of multiple responses in javascript?

Time:05-12

The code below outputs numeric data

          var meshInstances = entity.model.meshInstances;
          var mat;
          for(var i = 0; i < meshInstances.length; i  ) {
     //  mat = meshInstances[i].mesh.vertexBuffer.numVertices;
          mat = parseFloat(meshInstances[i].mesh.indexBuffer[0].numIndices / 3);
console.log(mat);

enter image description here

How to output this data as a sum of all responses? 1 Line with sum

CodePudding user response:

Thanks Anas Abdullah Al Changed it a little and it worked, was getting 'NaN' previously

var meshInstances = entity.model.meshInstances;
var mat;
var total = 0;
for (var i = 0; i < meshInstances.length; i  ) {
    //  mat = meshInstances[i].mesh.vertexBuffer.numVertices;
    mat = parseFloat(meshInstances[i].mesh.indexBuffer[0].numIndices / 3);
    total  = mat;
}
console.log(total);

CodePudding user response:

You can do something like this...

var meshInstances = entity.model.meshInstances;
var mat, total;
for (var i = 0; i < meshInstances.length; i  ) {
    //  mat = meshInstances[i].mesh.vertexBuffer.numVertices;
    mat = parseFloat(meshInstances[i].mesh.indexBuffer[0].numIndices / 3);
    total  = mat;
}
console.log(total);
  • Related