Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By medoman
#830249
hello all
i need to know what is wrong in this calculation? i need to calculate the 6 cloulmns numbers to be in the total field in 7th columns, the poroblem that when one field left empty the sum dont work. i need the code work even if level1 or 2 or 3 or 4 or 5 is empty


$("form").setOnchange(function(field, newVal, oldVal) {
var totalRows = $("#scores").getNumberRows();
for (var i = 1; i <= totalRows; i++) {
level1 = parseFloat($("#scores").getValue(i, 2));
level2 = parseFloat($("#scores").getValue(i, 3));
level3 = parseFloat($("#scores").getValue(i, 4));
level4 = parseFloat($("#scores").getValue(i, 5));
level5 = parseFloat($("#scores").getValue(i, 6));


total =level1 + level2 + level3 + level4 + level5;

$("#scores").setValue(total, i, 7);

}
});
By Ironguts25
#830267
Best way to do this is to check for a non numeric value for each variable being added using isNaN method and then use the Ternary operator to replace it with a zero if it finds a NaN value. Here's the code:

total = (isNaN(level1) ? 0 : +level1) + (isNaN(level2) ? 0 : +level2) + (isNaN(level3) ? 0 : +level3) + (isNaN(level4) ? 0 : +level4) + (isNaN(level5) ? 0 : +level5);

Let me know how if goes.
What's SAP FICO?

Trustworthy and skill-building, each of these actu[…]

To convert MBOX to PST, start by downloading and i[…]

My Assignment Services stands out as one of the be[…]

Erectile Dysfunction, commonly known as impotence,[…]