Performing Basic Math Using Scripting: Add, Subtract, Multiply, and Divide

Scripting and Other Custom Solutions

We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for Alchemer features and functionality ideas that might meet your needs. Beyond this, check out our Professional Services; these folks have the scripting chops to help you to achieve what you are looking for!

Do you need to perform basic math within a response? You can do so using our custom scripting. Because our scripting language is based on Lua any of the operators that are available in Lua are available in our scripting language, and thus, in our surveys! Learn more about Lua arithmetic operators.

Getting Started: Get Familiar with getvalue and setvalue

getvalue and setvalue are your friends when performing math in surveys; get familiar with these functions before you jump into doing math.

getvalue* returns the value (reporting value) of an answered question (or null, if not answered). Chances are, when performing math in a survey, you'll want to pull one or more values from a question to use in your arithmetic. For single answer questions, a single value is returned. If you are looking to use the value from a more complex question this can get quite a bit trickier. Check out the getvalue function documentation to learn more.

*getvalue can be used to pull data from questions on previous pages only.

setvalue** populates the response value of the given question. Chances are, when performing math in a survey, you'll want to use the result elsewhere in the survey; using setvalue you can store this data in a hidden value or other question for later use or reporting. The setvalue portion of the script below can be used to populate a hidden value, textbox, essay, radio button, drop-down menu, slider and image choice question. If you wish to populate other questions this can get quite a bit trickier. Check out the setvalue function documentation to learn more.

**setvalue can be used to set data in questions on any page.

These features are frequently used with the continuous sum question type. For details on continuous sum questions refer to the documentation below:

Example

Let's say, for example, you want to multiply the number of shirts the customer orders (entered in a Textbox question in a survey) by the price so you can get a total amount due.

Check it out in an example survey!

OR

Add a survey with this script to your account.

In the below script we pull the value of the number of shirts Textbox multiply it by the price and set the value of the Total Amount Due Textbox question.


numberofshirts = getvalue(5) --Get value of first textbox
shirtprice = 15 --Replace 15 with amount of shirt

totalamountdue = (numberofshirts * shirtprice) --[[Multiply the first textbox times the shirt amount--]]
  
setvalue(6,totalamountdue) --Change 6 to ID of Hidden Value or Textbox 
Basic Standard Market Research HR Professional Full Access Reporting
Free Individual Team & Enterprise
Feature Included In