All Collections
Modifying and Building Activities
How to Create an Expression that Evaluates Within a Text Box
How to Create an Expression that Evaluates Within a Text Box

Use expressions to change what students see in answer choices, teacher instructions, and more.

Trevor Register avatar
Written by Trevor Register
Updated over a week ago


Instructors can use expressions, including conditional if/then statements to change what students see as answer choices in a multiple choice question, teacher instructions, questions prompts, and more. They can be combined with Variables and Numerical Questions, Collection Variables, Multiple-Choice Questions to create powerful individualized experiences for students. These expressions can changed based on any number of factors including responses to previously answered questions, their assigned trial for an interactive video, a randomly selected number, and so much more!

How to write an expression

Consider the following example:

You've set up a kinematics activity that asks students to solve kinematics questions by selecting the correct multiple choice answer. The first question asks students to calculate the acceleration of a car while accelerating from 0 m/s to a final velocity in a given amount of time. You've set up variables for the final velocity and time, and you want the choices for the multiple to evaluate to different numbers based on a) students using the correct equation and b) students making several common mistakes.

You'd like the answer choices to evaluate using each of these formulas:

  • $velocityFinal/$time - the correct answer

  • $velocityFinal*$time

  • $time/$velocityFinal

  • $velocityFinal/$time^2

If a student had been assigned 10 m/s and 2 s, these answers would show as:

  • 5 m/s/s

  • 20 m/s/s

  • 0.2 m/s/s

  • 2.5 m/s/s

But if another student had been assigned 20 m/s and 2 s:

  • 10 m/s/s

  • 40 m/s/s

  • 0.1 m/s/s

  • 5 m/s/s

Here's how you'd set up the correct answer choice. Each additional choice would simply use a different formula.

Everything inside of the brackets here [expr: insert expression here] will evaluate once the student loads the question. The other answer choices are written similarly but with a different expression inside the brackets. Here's how the question would appear to one student.

...while another student, who'd be assigned different values for the final velocity and time, would see...

NOTE: Make sure you check the Randomize Choice Order box. Otherwise the correct answer choice will always be in the same position (1st,2nd, etc.) on every student's response!

To make edits, click the expression to open the expression editor. Click and drag the bottom-right corner to expand the editor window. Once you're done, click Update to save your changes.

What is a conditional expression?

A conditional expression is an if/then statement that determines what happens based on a set of inputs and conditions. The condition must be something that can be considered either true or false. Consider the following situation:

Students are tasked with using one of our interactive videos to predict the amount of time it will take for a ball to travel a specific distance. Depending on the trial assigned to a student and the accuracy of their measurements, they may predict a longer or shorter time when compared to the measured value. You can use a conditional expression to automatically grade a multiple choice question asking whether or not friction could explain the discrepancy.

A student states that the difference in predicted and measured times could be accounted for by taking friction into account. Is this a reasonable explanation?

  • Yes

  • No

Whether the correct answer is yes or no depends on a student's predicted time as compared to the measured value. In plain English, a conditional statement for this situation might look like this:

If a student's predicted time is less than their measured time, the correct answer is "yes." If not, the answer is "no."

The next section will teach you how to turn this into a statement that can be understood and evaluated by Pivot Interactives.

How to write a conditional expression

Here's how we can turn this conditional expression into something that Pivot Interactives can understand and evaluate. Assume that two variables based on student responses to previous questions, $measured and $predicted, have been defined.

[expr: $measured < $predicted? "yes": "no"]

Here's how to break this down:

  • [expr: ] - Everything inside the brackets will be interpreted as an expression

  • $measured < $predicted? - Is $measured is less than $predicted?

  • "yes" - If yes, this expression outputs "yes"

  • "no" - If no, this expression outputs "no"

Note that there's a colon separating "yes" from "no" and that anything meant to be interpreted as text must be inside of double quotes.

Here's how this expression would be used in a Multiple Choice Question

The prompt uses [$predicted] and [$measured] to display the previously entered values to the students.

Two answer choices are needed here since there are two possible answers, yes and no. The first answer choice contains the following expression and will always output the correct answer:

[expr: $measured < $predicted? "yes": "no"]

The second answer choice contains the opposite and will always output the incorrect answer. Note that the less-than sign has become a greater-than one.

[expr: $measured > $predicted? "yes": "no"]

Since the top answer choice always displays the correct answer, the Correct Answer box is selected. Randomize Choice Order is also selected because, otherwise, the first selection will always be the correct one. Here are a few screenshots of what this might look like for students.

In the two examples above, each student's measured value is less than the predicted one, thus no is the correct answer. Because Randomize Choice Order has been selected, it would be the first choice for one student but the second choice for another.

In the last example above, the student's measured value is greater than their predicted one, so the correct answer is yes.

Additional Examples

Consider the following situation:

In a previous question, students are given a velocity vs. time graph and asked to calculate the acceleration and input their answer into a Numerical Question. Using Collection Variables, students are randomly assigned one of several graphs. These graphs may have both positive and negative accelerations with positive, negative, or both velocities throughout. Two examples of what students could see are shown below.

The answer they input is then stored in a variable called $acceleration.

Imagine that we'd like to assess a student's understanding of positive and negative acceleration and whether that means the object is always speeding up or slowing down by evaluating a hypothetical student's argument. Further suppose that you'd like different question prompts, but ones that are both based on the student's acceleration calculation and the argument posed by the student is always incorrect. We can do that using an expression!

Here's how this would read in plain English:

If the student enters a positive number for their acceleration calculation, I want the student's argument to pose that positive accelerations always mean speeding up. If their number is negative, I want the argument to pose that negative acceleration always means slowing down.

Here's what you'd input into the Prompt box for the Open Ended Question:

Since the acceleration of the object is

[expr: $acceleration>0? "positive": "negative"]

this must mean that the object is

[expr: $acceleration>0? "speeding up.": "slowing down."]

Is this student correct or incorrect? Explain your reasoning.

Note that in this example what the question prompt displays is based on what the student entered in as their acceleration calculation, which may not necessarily match the graph.

Writing advanced expressions

Expressions in Pivot Interactives use the math.js library, so any valid math.js expression will work. You're also able to use any of the math.js functions, including:

  • round(x, n) - round a number x to n decimal places.

  • ceil(x), floor(x) - round a number x up or down, respectively.

  • abs(x) - calculate the absolute value of x.

  • sqrt(x) - calculate the square root of x.

You can also "nest" expressions inside of one another to handle multiple if/then conditions:

[expr: $a==$b? "a=b" : sqrt($c) > $d? "square-root of c > d": "square-root of c < d"]

This reads in plain English as:

If a is equal to b, then output a=b. If it's not, then check if the square-root of c is greater than d. If that's true, then output squre-root of c > d, otherwise output square-root of c < d.

Did this answer your question?