Range #

new Range(start, end, [step]) #

Create a range. A range has a start, step, and end, and contains functions to iterate over the range.

A range can be constructed as:

const range = new Range(start, end) 
const range = new Range(start, end, step) 

To get the result of the range:

range.forEach(function (x) {
    console.log(x) 
}) 
range.map(function (x) {
    return math.sin(x) 
}) 
range.toArray() 

Example usage:

const c = new Range(2, 6)        // 2:1:5
c.toArray()                      // [2, 3, 4, 5]
const d = new Range(2, -3, -1)   // 2:-1:-2
d.toArray()                      // [2, 1, 0, -1, -2]
Param Type Description
start number included lower bound
end number excluded upper bound
[step] number step size, default value is 1

range.size() ⇒ Array.<number> #

Retrieve the size of the range. Returns an array containing one number, the number of elements in the range.

Kind: instance method of [Range](#Range)
Returns: Array.<number> - size

range.min() ⇒ number | undefined #

Calculate the minimum value in the range

Kind: instance method of [Range](#Range)
Returns: number | undefined - min

range.max() ⇒ number | undefined #

Calculate the maximum value in the range

Kind: instance method of [Range](#Range)
Returns: number | undefined - max

range.forEach(callback) #

Execute a callback function for each value in the range.

Kind: instance method of [Range](#Range)

Param Type Description
callback function The callback method is invoked with three parameters: the value of the element, the index of the element, and the Range being traversed.

range.map(callback) ⇒ Array #

Execute a callback function for each value in the Range, and return the results as an array

Kind: instance method of [Range](#Range)
Returns: Array - array

Param Type Description
callback function The callback method is invoked with three parameters: the value of the element, the index of the element, and the Matrix being traversed.

range.toArray() ⇒ Array #

Create an Array with a copy of the Ranges data

Kind: instance method of [Range](#Range)
Returns: Array - array

range.valueOf() ⇒ Array #

Get the primitive value of the Range, a one dimensional array

Kind: instance method of [Range](#Range)
Returns: Array - array

range.format([options]) ⇒ string #

Get a string representation of the range, with optional formatting options. Output is formatted as ‘start:step:end’, for example ‘2:6’ or ‘0:0.2:11’

Kind: instance method of [Range](#Range)
Returns: string - str

Param Type Description
[options] Object | number | function Formatting options. See lib/utils/number:format for a description of the available options.

range.toString() ⇒ string #

Get a string representation of the range.

Kind: instance method of [Range](#Range)

range.toJSON() ⇒ Object #

Get a JSON representation of the range

Kind: instance method of [Range](#Range)
Returns: Object - Returns a JSON object structured as: {"mathjs": "Range", "start": 2, "end": 4, "step": 1}

Range.parse(str) ⇒ [Range](#Range) | null #

Parse a string into a range, The string contains the start, optional step, and end, separated by a colon. If the string does not contain a valid range, null is returned. For example str=’0:2:11’.

Kind: static method of [Range](#Range)
Returns: [Range](#Range) | null - range

Param Type
str string

Range.fromJSON(json) ⇒ [Range](#Range) #

Instantiate a Range from a JSON object

Kind: static method of [Range](#Range)

Param Type Description
json Object A JSON object structured as: {"mathjs": "Range", "start": 2, "end": 4, "step": 1}
Fork me on GitHub