Skip to main content

Basic Functions

Updated over a month ago

Mathematical/Numeric functions

absolute(input, precision)

Computes input number to absolute

Argument

Required

Description

input

✔️

A Number value input

precision

✖️

A Number value precision. Default = 0

Returns

An absolute number

ceil(input, precision)

Computes input number rounded up to precision

Argument

Required

Description

input

✔️

A Number value input

precision

✖️

A Number value precision. Default = 0

Returns

The rounded up number

Example

ceil(1.2) -- returns 2
ceil(-3.14) -- returns -3
ceil(1.5222, 2) -- returns 1.53

floor(input, precision)

Computes input number rounded down to precision

Argument

Required

Description

input

✔️

A Number value input

precision

✖️

A Number value precision. Default = 0

Returns

The rounded down number

Example

floor(1.2) -- returns 1
floor(-3.14) -- returns -4
floor(1.5222, 2) -- returns 1.52

exp(input)

Computes 𝑒 to the power of input number

Argument

Required

Description

input

✔️

A Number value input

Returns

𝑒 raised to the power of input number.

Example

exp(1) -- returns 2.718281828459045
exp(2) -- returns 7.38905609893065
exp(-1) -- returns 0.36787944117144233
exp(0) -- returns 1

max(listOfNumbers)

Computes the largest value

Argument

Required

Description

listOfNumbers

✔️

A table/array of numbers

Returns

Returns the largest value number.

Example

max({4, 2, 8, 6, 7}) -- returns 8
max({0.5, 0.1, 9, 6.5, 0.7}) -- returns 9
max({1, 1, 1, 1, 1,0}) -- returns 1
max({1}) -- returns 1

min(listOfNumbers)

Computes the smallest value

Argument

Required

Description

listOfNumbers

✔️

A table/array of numbers

Returns

The smallest value number.

Example

min({4, 2, 8, 6, 7}) -- returns 2
min({0.5, 0.1, 9, 6.5, 0.7}) -- returns 0.1
min({1, 1, 1, 1, 1,0}) -- returns 0
min({1}) -- returns 1

pow(base, power)

The value of a base raised to a power.

Argument

Required

Description

base

✔️

A Number value base

power

✔️

A Number value power

Returns

The value of base number raised to the power number.

Example

pow(7, 3) -- returns 343
pow(4, 0.5) -- returns 2
pow(7, -2) -- returns 0.020408163265306124

convertToString(number)

Converts a valid number, string, object or an array to a string representation.

Argument

Required

Description

number

✔️

A Numeric or Text value

Returns

The string representation of a numeric, string, object or an array

Example

convertToString("TeSt") -- returns "TeSt"
convertToString(1234567891) -- returns "1234567891"
convertToString({key: "value"}) -- returns '{"key": "value"}
convertToString([1, "Rhino"]) -- returns '[1, "Rhino"]'
convertToString(null) -- returns 'null'
convertToString(undefined) -- returns 'null'

String functions

uppercase(string)

Transforms the string to uppercase

Argument

Required

Description

string

✔️

A Text value

Returns

The transformed string.

Example

uppercase("hello world") -- returns HELLO WORLD
uppercase("hello-world") -- returns HELLO-WORLD

lowercase(string)

Transforms the string to lowercase

Argument

Required

Description

string

✔️

A Text value

Returns

The transformed string.

Example

lowercase("hello world") -- returns hello world
lowercase("hello-world") -- returns hello-world

substring(string, lowerBound, length)

Extracts a sub-string starting at the specified index (based 1), with a given length

Argument

Required

Description

string

✔️

A Text value

lowerBound

✔️

A Number value

length

✖️

A Number value

Returns

The sub-string.

Example

substring(abcde, 2, 3) -- returns bcd
substring(abcde, 2) -- returns bcde
substring(abcde, 3, 10) -- returns cde
substring(abcde, 5) -- returns e

isMatch(string, regexp)

Extracts a sub-string starting at the specified index (based 1), with a given length

Argument

Required

Description

string

✔️

A Text value

regexp

✔️

A Text regular express pattern

Returns

The Boolean value of the match operation.

Example

isMatch("rival@rival.com", "\\w+@[a-zA-Z_]+\\.[a-zA-Z]{2,3}$") -- returns true
isMatch("rival@", "\\w+@[a-zA-Z_]+\\.[a-zA-Z]{2,3}$") -- returns false

convertToNumber(string)

Converts a valid string representation of a number or valid number to a number.

Argument

Required

Description

string

✔️

A Text or Numeric value

Returns

The numeric representation of a string or numeric value

Example

convertToNumber(3) -- returns 3
convertToNumber("5") -- returns 5
convertToNumber("123,456") -- For EU numeric string returns 123.456
convertToNumber("3bac") -- For alphanumeric string returns null
convertToNumber("1,234.56") -- For formatted string returns null
convertToNumber({key: "value"}) -- For object returns null
convertToNumber([{someKey: 'someValue'}, 'someOtherContent']) -- For array returns null
convertToNumber(null) -- For null returns null
convertToNumber(undefined) -- For undefined returns null

Date Functions

getCurrentDateTime()

Get the current date time in UTC format and in the UTC time zone

Argument

Required

Description

Returns

The current date time

Example

getCurrentDateTime() -- returns: 2024-01-16T22:57:09.948Z

getDateTimeDifference(date1, date2, unitOfTime)

Extracts a sub-string starting at the specified index (based 1), with a given length

Argument

Required

Description

date1

✔️

A Text UTC formatted

date2

✔️

A Text UTC formatted

unitOfTime

✖️

One of the following values: "years", "quarters", "months", "weeks", "days", "hours", "minutes", "seconds" and "milliseconds" (default)

Returns

The difference between two date times

Example

currentDT = getCurrentDateTime()
engagementStartTime = getEngagementStartDate()
getDateTimeDifference(currentDT, engagementStartTime)

formatDate(date, format)

Formats the date in the specific pattern

Argument

Required

Description

date

✔️

A Text UTC formatted

format

✖️

A Text valid format pattern string. The complete list of options for formatting can be found here. If no format string is provided, it returns the default ISO8601 formatted date time

Returns

The formatted date in the specific pattern

Example

jan1st2024Noon = '2024-01-01T12:00:00.000Z'
formatDate(jan1st2024Noon, "MMMM d'st' yyyy, h:mm:ss a") -- returns: January 1st 2024, 12:00:00 PM
formatDate(jan1st2024Noon, "ccc") -- returns: Monday
formatDate(jan1st2024Noon, "MMM d'st' yy") -- returns: Jan 1st 24
formatDate(jan1st2024Noon, "yyyy-MM-dd") -- returns: 2024-01-01
formatDate(jan1st2024Noon, "h:mm:ss a") -- returns: 12:00:00 PM
formatDate(jan1st2024Noon, "h:mm a") -- returns: 12:00 PM
formatDate(jan1st2024Noon, "MMMM d'st' yyyy") -- returns: January 1st 2024
formatDate(jan1st2024Noon, "yyyy 'escaped' yyyy") -- returns: 2024 escaped 2024
formatDate(jan1st2024Noon) -- returns: 2024-01-01T12:00:00.000Z

Arrays/Tables

concat(array1, array2)

Creates and return a single array by combining the two arrays passed as params. Order and items remain the same (no de-dupe).

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

Returns

The combined array/table

Example

list1 = {'moo', 'cow', false, 1, 1.0}
list2 = {'duck', 'quack', true, 1, 1.0, 5}
concat(list1, list2) -- returns {'moo', 'cow', false, 1, 1.0, 'duck', 'quack', true, 1, 1.0, 5}

includesAll(array1, array2)

Check if all items of array2 is present in array1

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

Returns

A Boolean true if all the data of array2 is present in array1, returns false otherwise.

Example

includesAll({"moo", "cow"}, {"moo", "cow"}) -- returns true
includesAll({"moo", "cow"}, {"cow"}) -- returns true
includesAll({"moo", "cow"}, {"cow", "horse"}) -- returns false

hasAllAndNoOther(array1, array2)

Check if all of the data of the array1 and no other data is present in array2

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

Returns

A Boolean true if all of the data of the array1 and no other data is present in array2, returns false otherwise.

Example

numList = {1, 2}
hasAllAndNoOther(numList, {1}) -- returns false
hasAllAndNoOther(numList, {1, 2}) -- returns true
hasAllAndNoOther(numList, {2, 1}) -- returns true
hasAllAndNoOther(numList, {2, 1, 3}) -- returns false
hasAllAndNoOther(numList, {4}) -- returns false

hasAndNoOther(array1, array2)

Check if at least one of the data of the array1 and no other data is present in array2

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

Returns

A Boolean true if at least one of the data of the array1 and no other data is present in array2, returns false otherwise.

Example

numList = {1, 2}
hasAndNoOther(numList, {1}) -- returns false
hasAndNoOther(numList, {1, 2}) -- returns true
hasAndNoOther(numList, {2, 1}) -- returns true
hasAndNoOther(numList, {2, 1, 3}) -- returns true
hasAndNoOther(numList, {4}) -- returns false

includesAny(array1, array2)

Check if any (one or more) of the data of array2 are present in array1.

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

Returns

A Boolean true if any (one or more) of the data of array2 are present in array1, returns false otherwise.

Example

numList = {1, 2}
includesAny(numList, {1}) -- returns true
includesAny(numList, {1, 2}) -- returns true
includesAny(numList, {2, 1}) -- returns true
includesAny(numList, {2, 1, 3}) -- returns true
includesAny(numList, {4}) -- returns false

hasNone(array1, array2)

Check if none of the data elements of array2 are present in array1.

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

Returns

A Boolean true if none of the data elements of array2 are present in array1, returns false otherwise.

Example

numList = {1, 2}
hasNone(numList, {1}) -- returns false
hasNone(numList, {1, 2}) -- returns false
hasNone(numList, {2, 1}) -- returns false
hasNone(numList, {2, 1, 3}) -- returns false
hasNone(numList, {4}) -- returns true
hasNone(numList, {5, 7}) -- returns true

insertAt(array1, array2, position)

Add a set of data (array2) with duplicates at a specific position in array1.

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

position

✔️

A Number index value

Returns

The modified array1 with added items of array2 at the specified position.

Example

insertAt({1,2,3,4,5}, {1}, 2) -- returns {1, 2, 1, 3, 4, 5}
insertAt({1,2,3,4,5}, {9, 11}, 0) -- returns {9, 11, 1,2,3,4,5}
insertAt({1,2,3,4,5}, {9, 11}, -1) -- returns {1,2,3,4,5,9,11}
insertAt({1,2,3,4,5}, {9, 11}, 89) -- returns {1,2,3,4,5,9,11}

union(array1, array2)

Concats an array/table of common elements with de duplication

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

Returns

Returns an array/table concatenated with de duplication.

Example

list1 = {'moo', 'cow', false, 1, 1.0}
list2 = {'duck', 'quack', true, 1, 1.0, 5}
union(list1, list2) -- returns {'moo', 'cow', false, 1, 1.0, 'duck', 'quack', true, 5}

remove(array, …elements)

Remove all elements from array that matches the data in array

Argument

Required

Description

array

✔️

An array of numbers, strings, Booleans or tables

...elements

✔️

one or more of numbers, strings, or Booleans

Returns

The array without the removed elements.

Example

remove({1.0, "cow", 3, "horse", 5.0, "duck", 7, "dog", 9.1, "lamb"},
1.0, 2, 3, 4, 5.0, 6, 7, 8, 9.1) -- returns {"cow", "horse", "duck, "dog", "lamb"}
remove({1.0, "cow", 3, "horse"}, 1.0, "cow", 3, "horse") -- returns {}

removeAt(array, index)

Removes the data at the specific index

Argument

Required

Description

array

✔️

An array of numbers, strings, Booleans or tables

index

✔️

A Number index value

Returns

The array without the element at the specified index

Example

removeAt({1, 2, 3, 4, 5}, 2) -- returns {1, 3, 4, 5}
removeAt({1, 2, 3, 4, 5}, -1) -- returns {1, 2, 3, 4}
removeAt({1, 2, 3, 4, 5}, 4) -- returns {1, 2, 3, 4}
removeAt({1, 2, 3, 4, 5}, 7) -- returns {1, 2, 3, 4}

shuffle(array)

Shuffles the elements of an array/table

Argument

Required

Description

array

✔️

An array of numbers, strings, Booleans or tables

Returns

The shuffled array. No dedupe

Example

shuffle({1, 2, 3, 4, 5}) -- returns an array of similar size and with same elements, but in a different order always

sort(array)

Sorts an array of number or strings

Argument

Required

Description

array

✔️

An array of numbers, strings, Booleans or tables

Returns

The sorted array

Example

sort({"Mercury", "Venus", "Earth", "Mars", "Jupiter"}) -- returns {"Earth", "Jupiter", "Mars", "Mercury", "Venus"}

subtraction(array1, array2)

Subtracts elements of one array from another

Argument

Required

Description

array1

✔️

An array of numbers, strings, Booleans or tables

array2

✔️

An array of numbers, strings, Booleans or tables

Returns

An array with any common values between array1 and array2 removed from array1

Example

subtraction({1, 2}, {1, 2, 3}) -- returns {}
subtraction({1, 2}, {1}) -- returns {2}
subtraction({1, 2, 3, 4, 5}, {7, 8, 9}) -- returns {1, 2, 3, 4, 5}

rand(array, size)

Randomize the elements of an array

Argument

Required

Description

array

✔️

An array of numbers, strings, Booleans or tables

size

✖️

Default is 1, if size is greater than the array size, all the elements would be returned in a random order

Returns

Returns the randomized array of a given size.

Example

inputNumbers = {4,2,8,6,7}
inputStrings = {'red', 'blue', 'yellow', 'green', 'orange'}
rand(inputNumbers) -- returns a table consisting of one of the inputNumbers values
rand(inputNumbers, 3) -- returns a table consisting of 3 of the inputNumbers values
rand(inputStrings, 3) -- returns a table consisting of 3 of the inputStrings values
rand(inputString, 11) -- returns a table consisting of all of the inputStrings values, but the order is random

size(array)

Computes length of an array

Argument

Required

Description

array

✔️

An array of numbers, strings, Booleans or tables

Returns

Returns size Number of the specified array.

Example

local allQuestions = getAllDatapoints()
size(allQuestions) -- If you have say 15 questions in your chat, it will return 15
size(allQuestions[2].Choices) -- Say, if the third question in this list is a Single Choice
-- or Multi Choice Question, and it has 5 choices, it will return 5
local mammals = {"Cow", "Sheep", "Horse", "Dolphin", "Giraffe"}
size(mammals) -- Returns 5
size(Single_Choice_1.Choices)
size(Multiple_Choice_1.Choices)
size(Multiple_Choice_1.Value)
size(Multiple_Choice_Profile_Attribute.Value)
size(Single_Choice_1.Choices)
size(Multiple_Choice_1.Choices)
size(Multiple_Choice_1.Value)
size(Multiple_Choice_Profile_Attribute.Value)

indexOf(array, element)

Finds the index of an element in the array

Argument

Required

Description

array

✔️

An array of numbers, strings, Booleans or tables

element

✔️

An element value for which index needs to be found

Returns

Returns the first index at which a given element can be found in the array, or -1 if it is not present

Ignores any additional parameters

Returns -1 if the first param is not an array

Example

local mammals = {"Cow", "Sheep", "Horse", "Dolphin", "Giraffe"}
indexOf(mammals, "Giraffe") -- Returns 5 as default arrays start with 1
indexOf(mammals, "Monkey") -- Returns -1 as monkey is not present
local customMammals = {[0]="Cow", "Sheep", "Horse", "Dolphin", "Giraffe"}
indexOf(customMammals, "Giraffe") -- Returns 4 as array is now explicitly set to start with 0
indexOf(mammals, "Giraffe", 1) -- Returns 5 as default arrays start with 1 and ignores the param 1
indexOf('not an array', "Giraffe") -- Return -1 as the first param is not an array

Did this answer your question?