JavaScript code for assigning different values to entry fields of constant sum | XM Community
Question

JavaScript code for assigning different values to entry fields of constant sum

  • 3 April 2019
  • 3 replies
  • 35 views

After trying to edit existing code I found from tangentially related questions, I figured it might be easier to just ask as I feel like there should be a more simplistic answer. I am trying to create a constant sum question that will calculate the total based on the dollar amounts assigned to each of four fields (i.e., entry field 1 is worth $5, entry field 2 is worth $10, etc.). I am essentially trying to perform a cross product calculation that will add to 100, but the number of "items" selected will actually sum to much less. JavaScript isn't my forte, but I do the ability to go in an edit some existing code if there is anything out there on this.

Any guidance on how this can be done?

3 replies

Userlevel 7
Badge +11
Maybe I'm misinterpreting your question but if you're just trying to sum the value of 4 fields, you can do this with Math Operations.

If that wasn't what you're looking for, can you explain more? 😀
I looked into Math Operations, but I don't think they'll do what I'm hoping for. Here is a screenshot to give an example:
!
Given $100, participants will choose the combination of items they want. The total of the selection right now sums to 5, whereas I'd like for it to show $55 (the sum of the products of the number selected and the price of each item). I guess it might be helpful if the total box started with the amount allocated and subtracted (e.g., $45 would show in the example above since $55 worth of the allocation has been used). Not sure if using a single column matrix would be a better format, but even then I am still faced with the problem of the calculation running correctly.
Userlevel 7
Badge +11
Ah, gotcha. If you want it to display the calculated field where you have it now in your screenshot, that looks like a custom code solution to me. I'm afraid I'm no coding genius, so I'll have to defer to our community members that are.

If you're okay with it displaying elsewhere (aka the next screen), you can use math operations and piped text.

Leave a Reply