Page 2 of 3

How can I average numbers from the database?

Posted: Thu Jan 12, 2017 3:48 pm
by Serhii Kulibaba

Could you show us screenshots with that code in the success event and it's response?


How can I average numbers from the database?

Posted: Thu Jan 12, 2017 7:23 pm
by Ellen Schlechter

How can I average numbers from the database?

Posted: Thu Jan 12, 2017 8:41 pm
by Serhii Kulibaba

Please use a JS below there to check the service response to be sure there are correct data:
prealert(JSON.stringify(data));/pre

Please here an example how to calculate average sum: http://stackoverflow.com/questions/10...

values of your array "grades" might be not a number, so just add a parseInt or parseFloat methods here


How can I average numbers from the database?

Posted: Thu Jan 12, 2017 10:34 pm
by Ellen Schlechter

I tried as you suggested, but the end result didn't change. Still comes back as NaN. Image


How can I average numbers from the database?

Posted: Fri Jan 13, 2017 10:05 pm
by Serhii Kulibaba

What values does you variable "grades" have? it seems it has wrong values... Could you send us it's value?


How can I average numbers from the database?

Posted: Fri Jan 13, 2017 10:58 pm
by Ellen Schlechter

Grades contains the values from the LSV 'average weight' that are mapped in this same service. The values are numbers and if you look at this screenshot, the pink is what the console logs as in that variable. https://d2r1vs3d9006ap.cloudfront.net...

This is how it looks in the database Image


How can I average numbers from the database?

Posted: Mon Jan 16, 2017 7:21 pm
by Ellen Schlechter

Do you have an update here?


How can I average numbers from the database?

Posted: Tue Jan 17, 2017 1:01 pm
by Serhii Kulibaba

There are empty values, which are not numbers. To prevent NaN error, please check these values before use them in the average value calculation.

Please change JS code:
pretotal += parseFloat(grades);/pre
to:
pretotal += parseFloat(grades || 0);/pre


How can I average numbers from the database?

Posted: Tue Jan 17, 2017 7:48 pm
by Ellen Schlechter

Switching out those lines of code didn't change anything. I still get the NaN error.


How can I average numbers from the database?

Posted: Wed Jan 18, 2017 9:36 am
by Serhii Kulibaba

Please send us a result of prealert(JSON.stringify(grades));/pre (screenshot).