Skip to content Skip to sidebar Skip to footer

Total Number Of Questions Divided By Total Opportunity. Only Use Javascript. Div Elements

I have already asked this for 1 list but I can't get it to work for 2 or more lists, the result always shows NaN. I have to calculate the average per list separately. the formula f

Solution 1:

In your example HTML, some of the course_eval_cell divs are empty. These will be empty strings when you fetch their innerHTML.

When you call split on those empty string, the result is just another empty string.

You attempt to assign the second character in that empty string to b but it will be undefined because the empty string has length 0.

You call parseInt on this value of undefined, which always results in NaN. Adding NaN to another number also results in NaN. In fact any other arithmetic operation involving NaN will result in NaN.

When you call toFixed(2) on NaN, it results in the string "NaN".

A possible fix for you might be to filter out empty strings so those values are not included in your calculation. You could try something like:

var cellValue = document.getElementsByClassName('course_eval_cell')[k];
if (cellValue) {
  [a, b] = cellValue.split('/');
  p1 += parseInt(a);
  p2 += parseInt(b);
}

Post a Comment for "Total Number Of Questions Divided By Total Opportunity. Only Use Javascript. Div Elements"