Range
A function that creates a range of numbers from start
to end
with a step
. This function is similar to the range function in Python.
๐ฌ Usage
๐ฉโ๐ปAPI
๐ป Arguments
Name | Description | Type | Required |
---|---|---|---|
start | Starting number | number | Yes |
stop | Stop number | number | No |
step | Step number | number | No |
โฉ๏ธ Returns
A generator number.
๐งช Playground
Source Code ๐
Source Code
/**
* Returns an generator of numbers from start to stop.
*
* @param start - The start of the range.
*
* @param stop - The end of the range.
*
* @param step - The step of the range.
*
* @see https://docs.python.org/3/library/functions.html#func-range
*
* @returns An generator of numbers.
*/
export function* range(
start: number,
stop?: number,
step = 1
): Generator<number> {
if (typeof stop === "undefined") {
stop = start
start = 0
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
return []
}
for (let i = start; i <= stop; i += step) {
yield i
if (i + step > stop) break
}
}
Last update: 2023-02-09
Authors: Mohamed-Kaizen