Perform Math Operations Based on User Input

From QualtricsWiki

Jump to: navigation, search

Qualtrics does not currently support a mathematical operations feature. However, if you are feeling adventurous, you can try using your javascript skills to perform simple math functions. (Below is an example you can refer to). If you would like to perform complex mathematical functions, this may not be possible. In this case, you may want to speak with your IT department to see if they can create some additional code for you. Note: If you are only want to perform addition and subtraction operations, we recommend using the Qualtrics scoring feature.
Scoring

How to perform a mathematical operation:

(This code functions for questions where the user is typing in a response; it will not work for multiple choice questions)

1. Define your variables in the header - Go to Edit Survey > Look and Feel > Header, and paste in the following line of code. Use the template below, replacing "average" with whatever you would like to call the variable.

<input type="hidden" id="average" name="ED~average" value="${e://Field/average}" />

2. Add embedded data to your survey flow - Give your embedded data field the same name as the variable you created in the header ('average', in the example above). You will not need to assign a value to this variable if prompted.

3. Perform the math function in the javascript editor of a question - On one of your questions, click the purple Advanced Question Options dropdown and select 'Add Javascript'. You can now give the variables a value (use the example below as a template for proper formatting) and perform mathematical operations. The example below pulls two answers (using the pipe text code that you can generate in the Rich Text Editor by clicking the {a} icon) and finds an average, which can then be returned to the user using piped text.

$('average').value = (${q://QID1/ChoiceNumericEntryValue/1} + ${q://QID1/ChoiceNumericEntryValue/2}) / 2;

Note that any questions you are pulling values from need to be on a separate page of the survey from where you insert your math operation, and this needs to be on a separate page from where you display any calculated values to the respondent.

If you would like an example survey with this process copied into your account, please contact support@qualtrics.com