OLI Dynamic Questions

OLI Dynamic Questions

  • Docs
  • API
  • Examples
  • Help

OLI API

The OLI module is available globally so scripts don't have to require or import anything to use it. To access functions from the module, simply use the OLI object.

if (OLI.almostEqual(1.11, 1.12, 10 ** -2)) {
    myVar = OLI.gcd(6, 60);
}

Optionally, individual OLI functions may be used globally within the script using ES6 Destructuring.

const { almostEqual, gcd } = OLI;

if (almostEqual(1.11, 1.12, 10 ** -2)) {
    myVar = gcd(6, 60);
}

Methods

  • OLI.almostEqual(a: number, b: number, difference: number = 10 ** -7)
  • OLI.gcd(x: number, y: number)
  • OLI.random(lower: number, upper: number, decimalPositions: number = 0)
  • OLI.randomArrayItem(arr: any[])
  • OLI.randomInt(lower: number, upper: number)
  • OLI.round(num: number, decimalPositions: number = 1)
  • OLI.toRadians(d: number)

Reference

OLI.almostEqual(a: number, b: number, difference: number = 10 ** -7)

Returns true if two numbers are "almost equal" within a given difference.

Optionally, a difference parameter can be provided for specifying how different two numbers can be and still be considered "almost equal". Note: The default value for difference is 10^-7.

This is often useful when needing to compare two floating point numbers that might not be exactly the same due to rounding.

For example:

const pi = Math.PI;
const piApproximation = 3.14159;

const isApproximately = OLI.almostEqual(pi, piApproximation, 10 ** -4);

module.exports = {
  isApproximately, // true
};

OLI.gcd(x: number, y: number)

Returns The greatest common divisor of two dividends.

For example:

const divisor = OLI.gcd(6, 60);

module.exports = {
  divisor // 6
};

OLI.random(lower: number, upper: number, decimalPositions: number = 0)

Returns a randomly generated number within a range, with the specified decimal positions. Note: The default value for decimalPositions is 0.

For example:

const myRandomNum = OLI.random(0, 10, 2);

module.exports = {
  myRandomNum // some random number x.xx
};

OLI.randomArrayItem(arr: any[])

Returns a randomly selected value from an array.

For example:

const items = ['apple', 'orange', 'tomato'];

const randomItem = OLI.randomArrayItem(items);

module.exports = {
  randomItem  // one of 'apple', 'orange' or 'tomato'
};

OLI.randomInt(lower: number, upper: number)

Returns a randomly generated integer with a range.

For example:

const randomInteger = OLI.randomInt(0, 10);

module.exports = {
  randomInteger // some integer in the range 0 to 10
};

OLI.round(num: number, decimalPositions: number = 1)

Returns the rounded value of a number. Note: The default value for decimalPositions is 1.

For example:

const preciseNumber = 1.12234;
const rounded = OLI.round(preciseNumber, 2);

module.exports = {
  rounded // 1.12
};

OLI.toRadians(d: number)

Returns the value of a number in radians.

For example:

const degrees = 180;
const radians = OLI.toRadians(degrees);

module.exports = {
  radians // 3.141592653589793
};
  • Methods
  • Reference
    • OLI.almostEqual(a: number, b: number, difference: number = 10 ** -7)
    • OLI.gcd(x: number, y: number)
    • OLI.random(lower: number, upper: number, decimalPositions: number = 0)
    • OLI.randomArrayItem(arr: any[])
    • OLI.randomInt(lower: number, upper: number)
    • OLI.round(num: number, decimalPositions: number = 1)
    • OLI.toRadians(d: number)
OLI Dynamic Questions
Docs
Getting StartedWriting Scripts
Community
Course Showcase (Coming soon)OLI Slack
More
OLIOLI Course EditorGitHubStar
Copyright © 2025 Carnegie Mellon University