Join the Team Forms community

R
A
T
C
G

Scoring With Multiple Choice Answers

Last updated 2 months ago
T
Tom

In this case Values 'a' and 'b' are the correct answer, which i would want to give a score of '1' for each of them.

I have tried the scripts below without any success

value = 
(['a'].includes(data.q1) ? 1 : 0) +
(['b'].includes(data.q1) ? 1 : 0);
value = 
  (data.q1 === 'a' ? 1 : 0 )
+ (data.q1 === 'b' ? 1 : 0 );
E
Erin Dwyer
Marked as solution

Hi Tom,

I think I would start with questioning why a data-grid is being used for this scenario? as users add additional rows how can you determine if the "value" column is correct? perhaps a table component or columns is actually what you need?

The data-grid is really intended for when you need a dynamic number of rows.

View full solution
E
T
1 comment·5 replies
E
Erin Dwyer

Hi Tom,

I think I would start with questioning why a data-grid is being used for this scenario? as users add additional rows how can you determine if the "value" column is correct? perhaps a table component or columns is actually what you need?

The data-grid is really intended for when you need a dynamic number of rows.

T
Tom

Hey Erin, This isn't a data grid. It's a select box component. The JS works with radio component which allows only one option to be selected, but the select box component which allows multiple selections doesn't seem to like the JS

E
Erin Dwyer

Hi Tom,

Looks like the screen shot you provided is of a data-grid, was that a mistake?

T
Tom

See screenshot

E
Erin Dwyer

Thanks for the additional screen shot, that makes a lot of sense now!

Important:
I noticed that the value column of each option in your select box is not unique. To prevent issues, you should make sure this is unique for each option (see my example below). I would also recommend copying the values I use in the screen shot below so that the JavaScript provided below will need minimal changes.

Solution:

The response to a select box component is stored as an object so accessing the response to each checkbox is done using data.selectBoxName.option syntax.

To calculate a score, you can use the following method:

  1. Add a number component into your form.

  2. In the calculated value property of the number component add the following JavaScript. Remember to adapt it to your specific scenario.

value = (data.selectboxes.option015 ? 1 : 0) + (data.selectboxes.option0075 ? 1 : 0)

Here's the end result:

T
Tom

Thanks Erin, Works perfectly