Nalini
Posts: 0
Joined: Tue Jan 14, 2014 2:49 pm

Calculate average from values in database, then show output in UI labels

Hello, I have several records in an appery database. I'd like to average the values in each of the two of the variables and show output for each using query service in two UI labels. I'd like the average to keep updating with each new record.

Can you point me to a javascript that will allow me to calculate the average. I'll also need some direction with invoking the script

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Calculate average from values in database, then show output in UI labels

To calculate an average, you would sum all the numbers and divide by the total count -- you would do this with JavaScript. We don't have this particular example for this, but it's basic JavaScript using numbers.

This is how to invoke JavaScript: http://docs.appery.io/documentation/b...

Nalini
Posts: 0
Joined: Tue Jan 14, 2014 2:49 pm

Calculate average from values in database, then show output in UI labels

i had already written my javascript but that documentation you referenced is pretty basic and does not tell me how I would execute code so that I could get average to show up on the 'label' component of the UI.

for(var x = 0; x < scores.length; x ++)
{
Sum = Sum + scores[x];
}
average = Sum / scores.length;

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Calculate average from values in database, then show output in UI labels

Hi,

Where did you add this code?

Nalini
Posts: 0
Joined: Tue Jan 14, 2014 2:49 pm

Calculate average from values in database, then show output in UI labels

Create new -- javascript --

The data is in my appery dbase, now i need to know how to view the average in the UI label

Nalini
Posts: 0
Joined: Tue Jan 14, 2014 2:49 pm

Calculate average from values in database, then show output in UI labels

Your support is really really frustrating. I know how to create databases and work with services. What I need help with is how to use the above javascript so that It will take the values of 1 variable in the database, calculate the average and them provide output in a label.

DO YOU HAVE A TUTORIAL THAT WILL SHOW ME HOW TO DO THAT??? I asked my question 15hrs ago and still no guidance... HELP!!!!

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Calculate average from values in database, then show output in UI labels

You would need to save an array (a database response) in localStorage: http://docs.appery.io/documentation/r...

An only difference that you add this code on Success event. Then calculate the average:precodevar val=0;
for (var i=0; i<data&#46;length; i++) {
val+=+data&#46;param;
};
val=val&#47;arr&#46;length;/code/preWhere 'val' is a result (average)
'param' is a name of a column for what we count the average.

Nalini
Posts: 0
Joined: Tue Jan 14, 2014 2:49 pm

Calculate average from values in database, then show output in UI labels

Hello, that documentation was not helpful, so I saved my database response variable in local storage the way I know how. It didn't work. Please help. I'm trying to do the following:

enter value into search input -- search will find all of variable 2 which correspond to search input

then js will find average of those values and show average on UI label. I've copied some images for reference. Image Image Image Image Image Image

Return to “Issues”