Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

How can I average numbers from the database?

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

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

How can I average numbers from the database?

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

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

How can I average numbers from the database?

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

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

How can I average numbers from the database?

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

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

How can I average numbers from the database?

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

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

How can I average numbers from the database?

Do you have an update here?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

How can I average numbers from the database?

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

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

How can I average numbers from the database?

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

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

How can I average numbers from the database?

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

Return to “Issues”