0

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

4
  • 1
    Add mat to a total variable, then output the variable after the loop is done. Commented May 11, 2022 at 16:50
  • 2
    total += mat; Commented May 11, 2022 at 16:50
  • make a sum variable and keep adding the new values to it and then print it Commented May 11, 2022 at 16:50
  • 1
    You could increment the value of mat by the parseFloat amount each time, rather than keep reassigning the value. Also, you could refactor the function with a Array.reduce method. Commented May 11, 2022 at 16:51

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.