My script is returning NAN and I want it to show 0 instead.
Using || 0 has achieved this however it now returns 100 instead of 0 for var percentQ. What is causing it to return 100?
$('#observationTotalRefresh').on('click', function () {
var
sumP = 0,
sumQ = 0,
countP = 0,
countQ = 0;
$('.observationPositive:not(:disabled)').each(function(){
countP++;
sumP += Number($(this).val());
});
$('.observationQuestionable:not(:disabled)').each(function(){
countQ++;
sumQ += Number($(this).val());
});
var percentP = 100/(1 + (sumQ/sumP)) || 0;
var percentQ = (100 - percentP) || 0;
$("#observationPositivePercentage").text(percentP.toFixed(0));
$("#observationQuestionablePercentage").text(percentQ.toFixed(0));
$("#observationTotal").text(sumP + sumQ);
});
If you do
|| 0forpercentP, you are ensuring thatpercentPis always a number. So,percentQwill always be100 - percentP; ifpercentPis 0, thenpercentQwill be 100.Your math for percentages seems oddly fishy, though. Perhaps you wanted
instead? Then, it’s simply