OLI Dynamic Questions

OLI Dynamic Questions

  • Docs
  • API
  • Examples
  • Help

›Writing Scripts

Getting Started

  • Overview
  • Requirements

Creating Questions

  • Workflow
  • Referencing Variables

Writing Scripts

  • Script Basics
  • Built-In Functions
  • Third-Party Libraries

Script Basics

The variables referenced in dynamic questions are defined in the script portion of the question.

The Basics

Learning JavaScript

Scripts are implemented using the JavaScript programming language. While an introduction to JavaScript is outside of the scope of this documentation, see this tutorial from CodeAcademy to get a basic understanding of the language.

Script Requirements

Each script must include a module.exports statement that lists the variables that the script defines. Other JavaScript variables can be declared in a script, but only those listed in the module.exports statement will be available as variable references in the question content. The following module.exports statement exposes two variables correct and distractor to OLI.

module.exports = {
  correct,
  distractor,
};

Beyond the module.exports statement, the only other requirement is that the script includes valid JavaScript statements that define each exposed variable. In many cases these definitions will be as simple as expressions that could be input into a calculator. For example, the following statement defines distractor in terms of variable correct using just addition and division.

const distractor = (correct + 10) / 2;

After adding a definition for variable correct to be a random number, the complete script looks like:

const correct = OLI.random(1, 10);
const distractor = (correct + 10) / 2;

module.exports = {
  correct,
  distractor,
};

Additional details

There are some additional details to keep in mind for users with a background in programming in JavaScript:

Evaluation environment

Scripts evaluate in a sandboxed runtime environment on the OLI servers. Currently this environment is running a Node.js version that is language compatible with version 8.2.1. See the Node.js language compatibiilty table for details on the specific language constructs that this version supports.

Scripts only evaluate on trusted OLI servers, never in a student's web browser. This eliminates the possibility that a student could inspect the source code to reverse engineer a correct answer.

Sandbox constraints

For security reasons, some constraints and limitations exist on what can be done within a script. Within a script, no access is available to any of the following:

  • The local file system (e.g. via the node fs module)
  • The network
  • Scheduling functions such as setInterval, setTimeout as setImmediate

A 300 millisecond execution time quota is allocated to each script. If the script does not finish evaluating after this time limit the sandbox environment terminates the script and declares it to be in error. This prevents scripts with accidental infinite loops from tying up system resources.

Scripts running in the sandbox also do not have access to the Node require method. The implication here is that neither built-in Node packages or arbitrary, third-party npm packages can be used within a script.

← Referencing VariablesBuilt-In Functions →
  • The Basics
    • Learning JavaScript
    • Script Requirements
  • Additional details
    • Evaluation environment
    • Sandbox constraints
OLI Dynamic Questions
Docs
Getting StartedWriting Scripts
Community
Course Showcase (Coming soon)OLI Slack
More
OLIOLI Course EditorGitHubStar
Copyright © 2025 Carnegie Mellon University