OLI Dynamic Questions

OLI Dynamic Questions

  • Docs
  • API
  • Examples
  • Help

›Mathematics

About

  • Overview

Statistics

  • Calculating the mean

Mathematics

  • Solve for x

Chemistry

  • Stoichiometry

Solve for x

Here is an example of dynamic question that asks the student to solve a dynamically generated quadratic equation for 𝑥.

// create a template equation that can be dynamically changed using val
const template = algebra.parse('(x + val)^2 = 0');

// generate an equation using a random value between 1 and 10 for val
const equation = template.eval({ val: OLI.randomInt(1, 10) });
const solution = equation.solveFor('x').toString();

// generate distractors based on the correct solution
const distractor1 = Number(solution) + 1;
const distractor2 = Number(solution) * -1;
const distractor3 = Number(solution) + 2;
const distractor4 = Number(solution) + 2 * -1;

module.exports = {
  equation: equation.toString(),
  equationLaTeX: algebra.toTex(equation),
  solution,
  distractor1,
  distractor2,
  distractor3,
  distractor4,
};

The above code snippet defines and exports seven variables (equation, equationLaTeX, solution , distractor1, distractor2, distractor3 and distractor4) that can then be referenced from any part of a question. This question leverages an open-source thrid-party library, Algebra.js. See Third-Party Libraries for more information.

Generating Distractors

Since we are generating our equation and solution dynamically in this example, it is useful to generate our distractors dynamically as well. Here we have 4 distractors, each of which are a slight variation of the correct solution. Different questions may call for different methods of generating distractors, or it may be sufficient to use a standard set of distractors and not generate them at all. This all depends to the nature of the question.

Be careful when generating distractors to not create a distractor that could possibly evaluate to the same value as the correct solution or another distractor. For example, in this script if solution evaluated to zero, distractor2 would be problematic because it would evaluate to zero as well leaving the student with two identical choices, only one of which is considered correct. We can avoid the issue here by limiting our range of random integers for val between 1 and 10 which we know will never produce a solution that is zero. The other distractors are generated using simple arithmetic which we can guarentee produce values that will not equal each other or the correct solution.

Tip

This example uses the Algebra.js function algebra.toTex() to create a LaTeX representation of the generated quadratic equation that can be rendered in any part of a question by surrounding the variable with double dollar signs $$@@equationLaTeX@@$$

← Calculating the meanStoichiometry →
OLI Dynamic Questions
Docs
Getting StartedWriting Scripts
Community
Course Showcase (Coming soon)OLI Slack
More
OLIOLI Course EditorGitHubStar
Copyright © 2025 Carnegie Mellon University