Formula Functions - CMUCTAT/CTAT GitHub Wiki

CTAT defines functions that can be used in JS formulas to match student input and customize hint and error messages in example-tracing tutors. These formulas can be used in the CTAT link editor. The functions can also be used on the right-hand side of Nools rules.

For more information on how to use functions in example-tracing tutors, see Generalizing an example-tracing tutor with formulas. Below are the functions provided by CTAT. Many of the functions below return null if any of their arguments are not proper values for the function.

Index

Numbers

These functions perform additional usual operations or tests on numbers. They usually can take either numbers or numeric strings as arguments and return numbers or booleans.

sum

sum(numbers...)

  • Arguments:

    • numbers...: <Number|String> - values to add, either as numbers or as strings; at least one argument is required; additional arguments are optional
  • Result: <Number> - the sum of all arguments

  • Description: Adds two or more values and returns the result.

  • Usage Exaples:

    sum("4", 6)

    => 10

mod

mod(number1, number2)

  • Arguments:

    • number1: <Number|String> - dividend; if not an integer, will be converted to integer by rounding
    • number2: <Number|String> - divisor (i.e. modulus); if not an integer, will be converted to integer by rounding
  • Result: <Number> - the remainder of number1 divided by number2

  • Description: Returns the remainder of number1 divided by number2, after rounding them to integers. Similar to remainder function.

  • Usage Exaples:

    mod("9.99", 4)

    => 2

isFactor

isFactor(number1, number2)

  • Arguments:

    • number1: <Number|String> - factor
    • number2: <Number|String> - whole
  • Result: <Boolean> - a boolean value indicating whether number1 is a factor of number2

  • Description: Returns a boolean value that tests whether number1 is a factor of number2, that is whether number2 % number1 == 0.

  • Usage Exaples:

    isFactor("5.1", 10.2)

    => true

isMultiple

isMultiple(number1, number2)

  • Arguments:

    • number1: <Number|String> - whole
    • number2: <Number|String> - factor
  • Result: <Boolean> - a boolean value indicating whether number1 is a multiple of number2

  • Description: Returns a boolean value that tests whether number1 is a multiple of number2, that is whether number1 % number2 == 0.

  • Usage Exaples:

    isMultiple("10.2", 5.1)

    => true

isUnit

isUnit(number)

  • Arguments:

    • number: <Number|String> - number to test
  • Result: <Boolean> - a boolean value indicating whether number is equal to 1

  • Description: Returns a boolean value that tests whether number is equal to 1.

  • Usage Exaples:

    isUnit("1.0")

    => true

isAny

isAny(number1, number2)

  • Arguments:

    • number1: <Number|String> - first number
    • number2: <Number|String> - second number
  • Result: <Boolean> - a boolean value indicating whether number1 is either a factor or a multiple of number2, or is a unit

  • Description: Returns a boolean value that tests whether number1 is either a factor or a multiple of number2, or is a unit. So it performs isFactor(number1, number2) || isMultiple(number1, number2) || isUnit(number1).

  • Usage Exaples:

    isAny("10.2", 5.1)

    => true

    isAny("5.1", 10.2)

    => true

    isAny("1.0")

    => true

gcf

gcf(number1, number2)

  • Arguments:

    • number1: <Number|String> - first number
    • number2: <Number|String> - second number
  • Result: <Number> - the the greatest common factor of number1 and number2

  • Description: Returns the largest number that divides evenly into both number1 and number2.

    gcf(12, 8)

    => 4

lcm

lcm(number1, number2)

  • Arguments:

    • number1: <Number|String> - first number
    • number2: <Number|String> - second number
  • Result: <Number> - the least common multiple of number1 and number2

  • Description: Returns the smallest number that is a multiple of both number1 and number2.

  • Usage Exaples:

    lcm(6, 4)

    => 12

inRange

inRange(number, floor = -Infinity, ceiling = Infinity)

  • Arguments:

    • number: <Number|String> - number to test
    • floor: <Number|String> - low end of the range
    • ceiling: <Number|String> - high end of the range
  • Result: <Boolean> - boolean indicating if the number is in the given range

  • Description: Checks whether a number is within the range specified by the arguments, inclusive. Returns false if number is null. Returns true if number is greater than or equal to floor and number is less than or equal to ceiling.

  • Usage Exaples:

    inRange(10, 0, 9.5) 

    => false

integerInRange

integerInRange(number, floor = -Infinity, ceiling = Infinity)

  • Arguments:

    • number: <Number|String> - number to test; will be rounded to integer
    • floor: <Number|String> - low end of the range; will be rounded to integer
    • ceiling: <Number|String> - high end of the range; will be rounded to integer
  • Result: <Boolean> - boolean indicating if the number is in the given integer range

  • Description: Checks whether an integer is within the range specified by the arguments, inclusive. Returns false if number is null. Returns true if number is greater than or equal to floor and number is less than or equal to ceiling, after rounding all arguments to integers.

  • Usage Exaples:

    integerInRange(10, 0, 9.5) 

    => true

Number String Formatting

These functions generate strings representing numbers in specific formats.

fmtDecimal

fmtDecimal(number, precision = 2)

  • Arguments:

    • number: <Number|String> - number to format
    • precision: <Number|String> - number (integer) of decimal places (optional, defaults to 2)
  • Result: <String> - formatted number string

  • Description: Returns a string representing a decimal number with the given precision. If precision is zero, omits the decimal point (i.e., rounds to integer). Precision defaults to 2.

  • Usage Exaples:

    fmtDecimal(5.559)

    => "5.56"

    fmtDecimal(3.10145, 3) == "3.101" 

    => true

fmtNormal

fmtNormal(number, precision = 6)

  • Arguments:

    • number: <Number|String> - number to format
    • precision: <Number|String> - number (integer) of significant digits (optional, defaults to 6)
  • Result: <String> - formatted number string

  • Description: Returns a string representing a number formatted as a calculator would: as an integer if no fractional part; else with up to 6 decimals, but no trailing zeros.

  • Usage Exaples:

    fmtNormal(1.33333334) 

    => "1.333333"

    fmtNormal("09.4559", 2)

    => "9.46"

fmtDollar

fmtDollar(number, flags = "")

  • Arguments:

    • number: <Number|String> - number to format
    • flags: <String> - "i" or "d", or both (optional, defaults to none)
  • Result: <String> - formatted number string

  • Description: Returns a string representing a decimal number with 2 digits to the right of the decimal. If the flags argument contains an "i", then the string only has the integer dollar amount without the trailing ".00". If flags contains a "d", then the string is prefixed with a dollar sign.

  • Usage Exaples:

    concat("You entered ", fmtDollar("11.015"), ".") 

    => "You entered 11.02."

    concat("You entered ", fmtDollar("3", "d"), ".") 

    => "You entered $3.00."

Number String Tests

These functions test whether their arguments represent numbers of specific formats or are equal to specific values.

dollarEquals

dollarEquals(number, values...)

  • Arguments:

    • number: <Number|String> - number to test
    • values...: <Number>... or <String>... - numeric values to test against
  • Result: <Boolean> - a boolean indicating whether the given number is equal to any of the values...

  • Description: Returns a boolean which indicates if the given number formatted as a dollar amount is equal to any of the values... formatted as dollar amounts.

  • Usage Exaples:

    dollarEquals(9.99, 10)

    => true

    dollarEquals(9.999, 10)

    => true

matchWithoutPrecision

matchWithoutPrecision(number, value)

  • Arguments:

    • number: <Number|String> - number to test
    • value: <Number|String> - numeric value to match against
  • Result: <Boolean> - boolean indicating if number matches value

  • Description: Returns a boolean indicating if number matches value; if number is more precise than value, it will be rounded before it is compared to value.

  • Usage Exaples:

    matchWithoutPrecision(9.99, 10) 

    => true

    matchWithoutPrecision(9.99, 9.999) 

    => false

matchWithPrecision

matchWithPrecision(number, value)

  • Arguments:

    • number: <Number|String> - number to test
    • value: <Number|String> - numeric value to match against
  • Result: <Boolean> - boolean indicating if number matches value

  • Description: Returns a boolean indicating if number matches value numerically and also has the same precision.

  • Usage Exaples:

    matchWithPrecision(0.5, .5) 

    => true

    matchWithPrecision(0.51, .5) 

    => false

constantsConform

constantsConform(expression, patterns...)

  • Arguments:

    • expression: <String> - arithmetic expression
    • patterns...: <String> - regular expressions; At least one pattern is required; additional patterns are optional.
  • Result: <Boolean> - boolean indicating if each number in the expression matches at least one of the patterns...

  • Description: Returns a boolean indicating if the numbers in the expression conform to one or more of the patterns.... This can be used, e.g., to verify that all the numeric values in an equation are either integers or dollar amounts ("d.dd").

  • Usage Exaples:

    constantsConform("15 + 8 = x", "\\d+")

    => true

    constantsConform("24-x = 7.50", "[0-9]+", "[0-9][.][0-9][0-9]")

    => true

constantsDollar

constantsDollar(expression)

  • Arguments:

    • expression: <String> - arithmetic expression
  • Result: <Boolean> - boolean indicating if numbers are either integers or dollar amounts

  • Description: Returns a boolean indicating if each number in expression is either an integer or a dollars-and-cents ("d.dd") value.

  • Usage Exaples:

    constantsDollar("25.00")

    => true

    constantsDollar("1.50 + 8")

    => true

    constantsDollar("1.5 + 8")

    => false

String Values

These functions test input strings for formats representing constants or variables.

isInteger

isInteger(number, stringOk = true)

  • Arguments:

    • number: <Number|String> - number to test
    • stringOk: <Boolean> - option to accept strings (defaults to true)
  • Result: <Boolean> - boolean indicating if number is an integer

  • Description: Returns a boolean that indicates if number can be interpreted as an integer. stringOk is an option to accept a numberic string as argument (defaults to true).

  • Usage Exaples:

    isInteger(3.0)

    => true

    isInteger(3.5)

    => false

    isInteger(10, false)

    => true

    isInteger("10", false)

    => false

isNumber

isNumber(number, stringOk = true)

  • Arguments:

    • number: <Number|String> - number to test
    • stringOk: <Boolean> - option to accept strings (defaults to true)
  • Result: <Boolean> - boolean indicating if number is a number

  • Description: Returns a boolean that indicates if number can be interpreted as a number. stringOk is an option to accept a numberic string as argument (defaults to true).

  • Usage Exaples:

    isNumber(34.56)

    => true

    isNumber("34.56")

    => true

    isNumber(6.5, false)

    => true

    isNumber("6.5", false)

    => false

isVar

isVar(string)

  • Arguments:

    • string: <String> - string to test
  • Result: <Boolean> - boolean indicating if string is a variable

  • Description: Returns a boolean that indicates if string can be interpreted as a single letter variable. The string can be padded with spaces.

  • Usage Exaples:

    isVar("x") 

    => true

    isVar(" x ")

    => true

    isVar("2")

    => false

    isVar("x2") 

    => false

Fraction Strings

These functions perform operations and tests on strings representing simple and mixed fractions.

makeFraction

makeFraction(sign = "", whole = 0, numerator, denominator = 1)

  • Arguments:

    • sign: <String> - a sign string (one of "+", "-", "")
    • whole: <Number|String> - the whole (integer) part of a mixed fraction
    • numerator: <Number|String> - the numerator
    • denominator: <Number|String> - the denominator
  • Result: <String> a string representing a fraction with the given components

  • Description: Returns a string which represents the fraction composed of the given arguments. sign, whole, and denominator are optional and default to "", 0, and 1 respectively. denominator can be missing only if whole is also missing. So if only two numbers are provided, they are interpreted as numerator and denominator. Usage Exaples:

    makeFraction("13", "687")

    => "13/687"

    makeFraction("-", "13", "687")

    => "-13/687"

    makeFraction("-", "3", "13", "687")

    => "-3 13/687"

getSign

getSign(fraction)

  • Arguments:

    • fraction: <String> - fraction string formatted as "nnn/ddd" or "iii nnn/ddd"
  • Result: <String> - the sign string

  • Description: Returns a string representing the sign of fraction, which is expected to represent a simple fraction ("nnn/ddd") or a mixed fraction ("iii nnn/ddd"). Returns an empty string if the sign is missing, or null if the argument is not a valid fraction representation.

  • Usage Exaples:

    getSign("13/687")

    => ""

    getSign("-3 4/5")

    => "-"

getWhole

getWhole(fraction)

  • Arguments:

    • fraction: <String> - fraction string formatted as "nnn/ddd" or "iii nnn/ddd"
  • Result: <String> - the whole string

  • Description: Returns a string representing the whole part of fraction, which is expected to represent a simple fraction ("nnn/ddd") or a mixed fraction ("iii nnn/ddd"). Returns "0" if fraction is a simple fraction, or null if the argument is not a valid fraction representation.

  • Usage Exaples:

    getWhole("13/687")

    => ""

    getWhole("-3 4/5")

    => "-3"

getNumerator

getNumerator(fraction)

  • Arguments:

    • fraction: <String> - fraction string formatted as "nnn/ddd" or "iii nnn/ddd"
  • Result: <String> - the numerator string

  • Description: Returns a string representing the numerator of fraction, which is expected to represent a simple fraction ("nnn/ddd") or a mixed fraction ("iii nnn/ddd"). Returns null if the argument is not a valid fraction representation.

  • Usage Exaples:

    getNumerator("13/687")

    => "13"

    getNumerator("-3 4/5")

    => "4"

getDenominator

getDenominator(fraction)

  • Arguments:

    • fraction: <String> - fraction string formatted as "nnn/ddd" or "iii nnn/ddd"
  • Result: <String> - the denominator string

  • Description: Returns a string representing the denominator of fraction, which is expected to represent a simple fraction ("nnn/ddd") or a mixed fraction ("iii nnn/ddd"). Returns null if the argument is not a valid fraction representation.

  • Usage Exaples:

    getDenominator("13/687")

    => "687".

    getDenominator("-3 4/5")

    => "5".

convertToMixed

convertToMixed(fraction)

  • Arguments:

    • fraction: <String> - fraction string to convert
  • Result: <String> - converted mixed fraction string

  • Description: Returns a string representing the same fraction value as fraction, but as a mixed fraction in case its value is larger than 1.

  • Usage Exaples:

    convertToMixed("4/33")

    => "4/33"

    convertToMixed("4/3")

    => "1 1/3"

    convertToMixed("4")

    => "4"

convertToImproper

convertToImproper(fraction)

  • Arguments:

    • fraction: <String> - fraction string to convert
  • Result: <String> - converted improper fraction string

  • Description: Returns a string representing the same fraction value as fraction, but as a simple fraction (no whole part) even if its value is larger than 1.

  • Usage Exaples:

    convertToImproper("4/33")

    => "4/33"

    convertToImproper("1 1/3")

    => "4/3"

    convertToImproper("4")

    => "4"

evaluateFraction

evaluateFraction(fraction)

  • Arguments:

    • fraction: <String> - fraction string to evaluate
  • Result: <Number> - number representing the value of fraction

  • Description: Returns a number that represents the value of the given fraction string interpreted as a fraction.

  • Usage Exaples:

    evaluateFraction("1 1/3")

    => 1.3333333333333333

    evaluateFraction("4/33")

    => 0.12121212121212122

simplifyFraction

simplifyFraction(fraction)

  • Arguments:

    • fraction: <String> - fraction string to simplify
  • Result: - <String> - simplified fraction

  • Description: Returns a string representing the simplified equivalent to the fraction argument. The result will keep the input fraction type, that is proper arguments will return a proper fraction and improper arguments will return an improper fraction. If the numerator is a multiple of the denominator it will return an integer.

  • Usage Exaples:

    simplifyFraction("44/33")

    => "4/3"

    simplifyFraction("1 22/44")

    => "1 1/2"

    simplifyFraction("1 66/33")

    => "3"

simplifiedFraction

simplifiedFraction(fraction)

  • Arguments:

    • fraction: <String> - fraction string to test
  • Result: <Boolean> - boolean to indicate whether fraction is simplified

  • Description: Returns a boolean that tests whether fraction represents a simplified fraction. So it performs the same computation as simplifiedFraction(fraction) === fraction. Mixed fractions are considered simplified if their fraction part cannot be simplified further.

  • Usage Exaples:

    simplifiedFraction("1 22/44")

    => false

    simplifiedFraction("1 1/2")

    => true

fractionEquals

fractionEquals(fraction1, fraction2)

  • Arguments:

    • fraction1: <String> - first fraction string to test
    • fraction2: <String> - second fraction string to test
  • Result: <Boolean> - boolean indicating if the two fractions are equal

  • Description: Returns a boolean testing whether fraction1 and fraction2 represent equal fractions. It works by evauating the two fractions and comparing the results. So it performs the same computation as evaluateFraction(fraction1) === evaluateFraction(fraction2).

  • Usage Exaples:

    fractionEquals("2/4", "1/2")

    => true

    fractionEquals("1 2/4", "1 1/2")

    => true

simplifiedFractionEquals

simplifiedFractionEquals(fraction1, fraction2)

  • fraction1: <String> - first fraction string to test
  • fraction2: <String> - second fraction string to test
  • Result: <Boolean> - boolean indicating if the two fractions are equal and simplified

  • Description: Returns a boolean testing whether fraction1 and fraction2 represent simplified equal fractions. It works by evauating the two fractions and comparing the results. So it performs the same computation as simplifiedFraction(fraction1) && simplifiedFraction(fraction2) && fractionEquals(fraction1, fraction2).

  • Usage Exaples:

    simplifiedFractionEquals("2/4", "1/2")

    => false

    simplifiedFractionEquals("1 2/4", "1 2/4")

    => false

    simplifiedFractionEquals("1/2", "1/2")

    => true

rationalEquals

rationalEquals(number1, number2)

  • Arguments:

    • number1: <Number|String> - first number to test
    • number2: <Number|String> - second number to test
  • Result: <Boolean> - boolean indicating if the two numbers are equal

  • Description: Returns a boolean indicating whether the two numbers represent equal values. The number arguments could represent fraction strings, in which case they are first evaluated and then compared. The comparison is done with function matchWithoutPrecision.

  • Usage Exaples:

    rationalEquals("2/4", 1/2)

    => true

    rationalEquals("2/4", 0.5)

    => true

Division Strings

These functions work with expression strings that represent a division.

getDividend

getDividend(expression)

  • Arguments:

    • expression: <String> - expression string representing a division
  • Result: <String> - dividend of the expression

  • Description: If the expression is a string representing a division between two numbers, it returns the dividend of that division operation. The difference between this function and getNumerator is that the two numbers can be any valid numbers, not just integers as in a fraction. If the expression does not represent a division of two numbers, it returns null.

  • Usage Exaples:

    getDividend("4/5")

    => "4"

    getDividend("4.5/5")

    => "4.5"

    getDividend("4.5/5e+2")

    => "4.5"

    getDividend("4.5")

    => null

getDivisor

getDivisor(expression)

  • Arguments:

    • expression: <String> - expression string representing a division
  • Result: <String> - divisor of the expression

  • Description: If the expression is a string representing a division between two numbers, it returns the divisor of that division operation. The difference between this function and getDenominator is that the two numbers can be any valid numbers, not just integers as in a fraction. If the expression does not represent a division of two numbers, it returns null.

  • Usage Exaples:

    getDividend("4/5")

    => "5"

    getDividend("4.5/5")

    => "5"

    getDividend("4.5/5e+2")

    => "5e+2"

    getDividend("5e+2")

    => null

divEvaluate

divEvaluate(expression)

  • Arguments:

    • expression: <String> - expression string representing a division
  • Result: <Number> - number representing the result of the division in expression

  • Description: If the expression is a string representing a division between two numbers, it returns the result of that division operation. The difference between this function and evaluateFraction is that the two numbers can be any valid numbers, not just integers as in a fraction. If the expression does not represent a division of two numbers, it returns null.

  • Usage Exaples:

    divEvaluate("4/5")

    => 0.8

    divEvaluate("4.5/5e+2")

    => 0.008

    divEvaluate("5e+2")

    => null

Quantity Strings

These functions deal with strings representing quantities, that is they are values followed by units.

matchQuantNumber

matchQuantNumber(quantity, value, precision)

  • Arguments:

    • quantity: <Number|String> - number or string representing a quantity, possibly having a unit
    • value: <Number> - target value to match
    • precision: <Number> - match precision
  • Result: <Boolean> - boolean testing whether the quantity number is in the range value ± precision

  • Description: The quantity argument can be a number or a string representing a number alone or followed by a unit. Returns a boolean indicating whether the number prefix in quantity is in the range value ± precision.

  • Usage Exaples:

    matchQuantNumber("3.5l", 3, 0.5)

    => true

    matchQuantNumber("3.5m3", 3.2, 0.2)

    => false

matchQuantUnit

matchQuantUnit(quantity, units...)

  • Arguments:

    • quantity: <Number|String> - number or string representing a quantity, possibly having a unit
    • units...: <String>... - unit strings to compare against
  • Result: <Boolean> - boolean testing whether the unit part of quantity matches any of the given units...

  • Description: The quantity argument can be a number or a string representing a number alone or followed by a unit. Returns a boolean indicating whether the unit suffix in quantity matches any of the unit strings given by units.... If the unit part is missing it will return false.

  • Usage Exaples:

    matchQuantUnit(3.5, "l", "m3")

    => false

    matchQuantUnit("3.5l", "l", "m3")

    => true

    matchQuantUnit("3.5m3", "l", "m3")

    => true

Strings

These are some generic functions on strings.

quote

quote(string)

  • Arguments:

    • string: <String> - string to quote
  • Result: <String> - quoted string

  • Description: Returns the given string embedded in double quotes (which show escaped by backslashes).

  • Usage Exaples:

    concat("How many ", quote(big), " dogs?") 

    => "How many \"big\" dogs?"

compact

compact(string)

  • Arguments:

    • string: <String> - string to compact
  • Result: <String> - compacted string

  • Description: Returns a string resulted from the given string with all space characters removed.

  • Usage Exaples:

    compact("How many \"big\" dogs?")

    => "Howmany\"big\"dogs?"

equalsIgnoreCase

equalsIgnoreCase(string1, string2)

  • Arguments:

    • string1: <String> - first string to compare
    • string2: <String> - second string to compare
  • Result: <Boolean> - boolean indicating whether the two strings are the same

  • Description: Returns a boolean that indicates whether the given strings are equal after being brought to the same case. So the comparison is case-insensitive.

  • Usage Exaples:

    equalsIgnoreCase("string", "String")

    => true

rm1coeff

rm1coeff(string)

  • Arguments:

    • string: <String> - string to change
  • Result: <String> - string with coefficients of 1 removed

  • Description: Returns a string that results by removing coefficients of 1 in front of single letter variables in the given string.

  • Usage Exaples:

    rm1coeff("1x + 1y")

    => "x + y"

    rm1coeff("3 + 1x")

    => "3 + x"

Sets

These functions perform operations on strings representing sets of values separated by a delimiter.

setMatches

setMatches(string1, string2, delimiter = ";")

  • Arguments:

    • string1: <String> - first string set
    • string2: <String> - second string set
    • delimiter:<String> - delimiter between items in the string sets (optional, defaults to ";")
  • Result: <Boolean> - boolean indicating if the two sets are equal

  • Description: If the two strings string1 and string2 represent set of items separated by delimiter, returns a boolean indicating if the two sets contain the same items.

  • Usage Exaples:

    setMatches("a,b,c", "c,b,a", ",")

    => true

    setMatches("a,b,c", "a,b", ",")

    => false

Point Strings

These functions work with strings or objects that represent 2-dimensional points.

pointInRange

pointInRange(point, x, y, xTolerance, yTolerance) -or-
pointInRange(point, point2, xTolerance, yTolerance)

  • Arguments:

    • point: <String> - string representing a point
    • x: <Number> - target x coordinate
    • y: <Number> - target y coordinate
    • point2: <String> - string representing target point
    • xTolerance: <Number> - target x tolerance
    • yTolerance: <Number> - target y tolerance
  • Result: <Boolean> - boolean indicating if point is close to point2 or {"x": x, "y": y} with the given tolerance

  • Description: point is interpreted as a string of the form {"x": xvalue, "y": yvalue}. The target point can be given either as two separate coordinates x and y or as a second point2 in the same format. Returns a boolean indicating whether the distance between the respective coordinates of the two points are withing the limits given by the respective tolerances.

  • Usage Exaples:

    pointInRange("{\"x\": 3.5, \"y\": 5.4}", 3, 5, 0.5, 0.5)

    => true

    pointInRange("{\"x\": 3.5, \"y\": 5.4}", "{\"x\": 3, \"y\": 5}", 0.5, 0.5)

    => true

hasPoint

hasPoint(list, point)

  • Arguments:

    • list: <Array> - list of points
    • point: <Object> - object representing a point
  • Result: <Boolean> - boolean indicating if the given list contains the given point

  • Description: point is interpreted as an object with properties 'x' and 'y'. list is an array of points in the same representation. Returns a boolean indicating whether the given list contains any point with the same coordinates as point.

  • Usage Exaples:

    hasPoint([{x: 1, y: 2}, {x: 3, y: 4}], {x: 3, y: 4})

    => true

    hasPoint([{x: 1, y: 2}, {x: 3, y: 4}], {x: 4, y: 5})

    => false

Words

These functions work on strings representing language words.

plural

plural(count, word, strip = "", add = "")

  • Arguments:

    • count: <Integer> - value specifying whether or not to pluralize word
    • word: <String> - word string
    • strip: <String> - characters to remove from the end of the word (optional, defaults to "")
    • add: <String> Characters to add to the end of the word (optional, defaults to "")
  • Result: <String> - singular or plural form of word

  • Description: Under control of a given count, return the singular or plural form of word. If the given count is greater than 1, make the given word plural by adding an "s". If the given count is greater than 1 and suffixes to remove and add are specified (strip and add), make the given word plural by removing strip and appending add.

  • Usage Exaples:

    plural(2, "apple") 

    => "apples"

    plural(2, "datum", "um", "a") 

    => "data"

Value Lists

These functions perform operations on lists of general values.

chooseRandomly

chooseRandomly(values...)

  • Arguments:

    • values...: <Any> - values from which to choose; at least one argument is required; additional arguments are optional
  • Result: <Any> - the randomly chosen value

  • Description: Returns one of two or more argument values chosen at random. Values can be strings, numbers, variables, etc.

  • Usage Exaples:

    chooseRandomly("x", "y", "z")

    => "y"

firstNonNull

firstNonNull(values...)

  • Arguments:

    • values...: <Any> - values to search; at least one argument is required; additional arguments are optional
  • Result: <Any> - the left-most non-null argument value

  • Description: Returns the first of the argument values which is non-null. If all values are null or undefined, null is returned.

  • Usage Exaples:

    firstNonNull(undefined, "5", 0)

    => "5"

valueOrZero

valueOrZero(values...)

  • values...: <Any> - values to search; at least one argument is required; additional arguments are optional
  • Result: <Any> - the left-most non-null argument value

  • Description: Returns the first of the argument values which is non-null. If all values are null or undefined, 0 is returned.

  • Usage Exaples:

    valueOrZero(null, 5)

    => 5

    valueOrZero(null, undefined)

    => 0

hasValue

hasValue(values...)

  • Arguments:

    • values...: <Any> - values to check; At least one argument is required; additional arguments are optional
  • Result: <Boolean> - boolean indicating if all values are non-null

  • Description: Returns a boolean testing whether all arguments have a value set (are non-null).

  • Usage Exaples:

    hasValue("1", "2") 

    => true

    hasValue("1", undefined) 

    => false

equals

equals(value, values...)

  • Arguments:

    • value: <Number|String>- value to check
    • values...: <Number|String>... - values to compare against
  • Result: <Boolean> - boolean indicating if value is equal to all of values...

  • Description: Returns a boolean testing whether value is equal to all of the items in values.... If value is a number or a string representing a number, the items in values... are also interpreted as numbers.

  • Usage Exaples:

    equals(0, "0")

    => true

    equals(0, "0", 1)

    => false

    equals("addition", "addition")

    => true

memberOf

memberOf(value, values...)

  • Arguments:

    • value: <Number|String>- value to check
    • values...: <Number|String>... - values to compare against
  • Result: <Boolean> - boolean indicating if value is equal to any of values...

  • Description: Returns a boolean testing whether value is equal to any of the items in values.... If value is a number or a string representing a number, the items in values... are also interpreted as numbers.

  • Usage Exaples:

    memberOf(0, "0", 1) 

    => true

    memberOf(2, "0", 1)

    => false

String, Number, and Math functions

CTAT supports directly all constants and functions defined in Javascript in classes String, Number, and Math. For a reference of these functions see links below:

W3Schools JS String Reference
W3Schools JS Number Reference
W3Schools JS Math Reference

MDN String Reference
MDN Number Reference
MDN Math Reference

  • Usage Examples:

    equals("5", sqrt(25)) 

    => true

    toLowerCase("E") 

    => "e"

Back to top

Next >> Formula Actions

⚠️ **GitHub.com Fallback** ⚠️