Calculating using states?

Hello! I was just about to post a question on how to do this after struggling for a while but I think I found a way. If it’s the wrong way, hope to get some improvements or better ways to achieve. If it’s a decent solution then hope it helps someone.

I have a “quiz” and I want to be able to calculate the score of the results. Each quiz question is out of 5. Some questions are optional so for the total score, I only want to take the sum of the questions answered / the total possible score amount for those questions answered.

For example, if there are ten questions in my quiz and I only answer four, it would be the sum of the four scores/20. If I answered 6, it would be the sum out of those 6/30. I thought it would be simple to say count sum of the inputs that are not empty or to do a search for the inputs that aren’t empty then sum those but those didn’t seem to be options.

Approach - Using custom states This is exactly where I started to realize after implementing that this might not go well with multiple users at a time, more on this towards the end.

My plan was to create a state for the total number of points given, then a state for the number of times a response was given both states of type number, default 0. So in my workflow, for the field for the quiz called Total points given, each time an input’s value was changed, it would take the value and add that to the state, capturing the total score given for that quiz.

There was also a step in the workflow to then update the state for the number of questions answered by adding +1 to that state whenever the input was changed. This value is saved under the field Number of questions answered. Then, after the quiz is submitted, along with everything else it’s creating and updating, I had a text element where I held the total number of questions answered and multiplied that by 5 (total points for each) to get the Total points possible.

To calculate the Overall Score (Total points given/total points possible), in the workflow after the results were saved (oh jeez I’m starting to feel crazy now), I had another step to update that thing with the following:

States are reset to 0 on page load.

So, it works with the tests that I ran but I suspect this is EXTRA. Would love to know if I completely missed the mark on a simpler approach. I’m also worried that using states might cause some problems. If more than one person is on this quiz at a time will that throw off the states? Thanks for sticking to the end.

Hello! States are actually absolutely your best bet here, especially if you’re concerned about keeping these results very individualized. States run client-side: this means the state is specific to a user’s browser, and cannot overlap with other users. It’s also quicker to set a state than to update your database, since the information doesn’t have to get sent to/received from the server. :slight_smile:

This topic was automatically closed after 70 days. New replies are no longer allowed.