Average Calucator

Hi.
I am creating a School Grade Assessment App and currently trying to calculate the average score of 4 inputs. How can I make it to give me the correct average even when one/two input(s) is empty?

I have done a setup Bubble | No-code apps
and used Math.js expression with 15 conditions
(if input 1 is empty & input 2,3,4 are not empty) ÷ 3 . So on and so on (15 conditions).

My method is really tedious as I need to calculate averages for about 100 different inputs based on different subjects. I cannot make 1500 conditions just to calculate averages.

Thanks for helping.

initialize your JS variables
let inputOne = ;
let inputTwo = ;

Load in your input values from bubble and .stringify() them.

create a function, isEmpty(string), that returns null if the parameter is an empty string.

Run all of your variables (inputOne, inputTwo, etc) through this function, and save the results into new variables (inputOneResult, inputTwoResult, etc).

Create another function, findTruthyValues(bool_list), that takes a list of truthy and falsey items, and .map() then onto a new list that only contains truthy values.

Create another function, calculateAverages(string_list), that takes in a list of strings, turns them into a list of numbers with parseFloat().

Then run all of your input variables (inputOneResult, inputTwoResult, etc) through these two functions.

You will need to specify logic in bubble that only lets the user hit “calculate” when the value inside the input is a number.

This was also written off the top of my head at 11pm, so im sure there are plenty of ways to refactor :slight_smile:

Hi you can try Bulk MVP plugin, they have bulk average function

Thanks for the response.
I am not sure what you mean by initialize your JS variables. Is it something I do in a specific plugin?

Thanks for the response.
I installed Bulk MVP plugin. I will play around with it and get back to you.

Have two custom states, one for the number of boxes filled in (_noOfBoxes) and the running total of those boxes (_totalValue). Have an initial action to set both to zero and four subsequent action steps, one for each box. Each box’s action step has an Only When conditional to check that the particular box is filled and sets the state of _noOfBoxes to _“noOfBoxes + 1” and adds the value to _totalValue eg “_totalValue + Box 1’s Value”. At the end you’d have another action step which performs the calculation of the average based on the custom states that have been calculated during the preceding steps. You can store the final average calculation in a custom state eg _avgCalc and have that bound to a text box to display it on the page. This is then done in 6 action steps. Hope it helps.

Interesting.
Let me try.
Thanks for the response.

im a little confused. How are you able to write conditionals on Math.js calculations without any javascript?

This is what I did but it is the long way.


Seems to me that I have been using Math.js the wrong way. :roll_eyes:
This is a test app editor https://bubble.io/page?name=index&id=averagevalues&tab=tabs-1

Ah OK you’re doing it using conditionals. My explain was using a standard workflow. To calculate an average don’t you just divide the total value by the number of values eg total / noOfValues? You can do that expression in a simple action step rather than needing MathJS eg:

2022-03-21_17-29-40

Anyway I’ve done a quick mock-up project for you here. Take a look at the Workflow. Hope it helps.

1 Like

It says User does not have permission to view.
I think you put the app on Private

Sorry about that, it’s sorted now for you to view.

1 Like

It works like magic, and the cool thing about it is that you did it without a plugin. This is Awesome :smiley:
I will study it and adjust to the app I am building.
Thanks a Million @gazinhio

1 Like