Sometimes when creating surveys in Qualtrics you wish to do some kind of math on a respondent’s answer. This post outlines a simple way to do so using javascript.

The procedure involves three main components. First, there are the questions themselves. These are the responses you wish to do some math operations on. Second, there are embedded data variables. You create these variables in the survey flow, and they allow you to save the values you calculate for later use. For example, you could use them in the survey flow (branch logic) or display the values as piped text. Third, there is the javascript. The javascript reads the answers of the questions, performs the math, and saves the embedded data variables.

**You can import this example into your own Qualtrics account by downloading this file.**

### 1. Create the questions

Create the questions. We will perform math operations on the responses to these questions. In the example, I have renamed the questions “A,” “B,” and “C.”

### 2. Create embedded data variable(s)

Create an embedded data variable in the survey flow as shown here. Create as many separate embedded data elements as you wish to store for future use. Future uses could be as branch logic in the survey flow, display logic for future questions, or to display the value you created as piped text.

In this example, I will be saving math calculations in the variables “utility” and “maxInput”. The other variables I am using like parameters. That is, I can change the math done by the javascript without changing the actual javascript by editing the values of these variables.

### 3. Create a question for the javascript

Create a question on a page *following* the input questions from 1. Click on the sprocket icon below the question name. This opens a pull-down menu. Select “Add javascript…”

Insert this code:

Qualtrics.SurveyEngine.addOnload(function() { // declare a variable you want to solve var utility; var maxInput; // Get the values of the given answers. // You will need to change QIDX to the // correct question number. Use piped text // to figure out what should go in the // quotation marks. var aInput = "${q://QID1/ChoiceGroup/SelectedChoices}"; var bInput = "${q://QID2/ChoiceGroup/SelectedChoices}"; var cInput = "${q://QID3/ChoiceGroup/SelectedChoices}"; // make sure javascript treats all the variables you // just read as integers aInput = parseInt(aInput); bInput = parseInt(bInput); cInput = parseInt(cInput); // Now get the mulitplier values var aMultiplier = "${e://Field/aMultiplier}"; var bMultiplier = "${e://Field/bMultiplier}"; var cMultiplier = "${e://Field/cMultiplier}"; var intercept = "${e://Field/intercept}"; // make sure javascript treats all the variables you // just read as integers aMultiplier = parseInt(aMultiplier); bMultiplier = parseInt(bMultiplier); cMultiplier = parseInt(cMultiplier); intercept = parseInt(intercept); // your javascript math goes here maxInput = Math.max(aInput,bInput,cInput); utility = intercept + (aInput * aMultiplier) + (bInput * bMultiplier) + (cInput * cMultiplier); // replace the "toReplace" div contents with the // calculation of utility document.getElementById("toReplace").innerHTML = utility; // save the utility value as an embedded data field Qualtrics.SurveyEngine.setEmbeddedData('utility',utility); Qualtrics.SurveyEngine.setEmbeddedData('maxInput',maxInput); });

#### What this does

This code reads in the answers from the questions. It then ensures that javascript treats the inputs as integers instead of strings. This ensures that 7 + 2 = 9, rather than 7 + 2 = 72, as would be the case for strings. The script then gets the parameter values I set earlier and ensures javascript treats these as integers as well.

Now finally, it does the math. There are two math functions performed. The first is a function to see what the highest response was to questions A, B, and C. The second equation plugs the user’s answers into a regression equation.

The line of code after the math functions displays the “utility” calculated in the regression equation in the question itself. Note that in order for this to work, you must create an HTML “div” element in the question with the “id” attribute of “toReplace.” You don’t need to display the output of the calculation in this question if you don’t want to. This is simply one way to do it.

Finally, the code saves the calculated values as embedded data. You can see usage examples as piped text in the block which follows it.