src/util/Util.js
/**
 * Create a sequence of integers.
 *
 * The sequence is such that the integer at index i is: start + step * i, with i >= 0 and start + step * i < stop
 *
 * <p> Note: this is a JavaScript implement of the Python range function, which explains the unusual management of arguments.</p>
 *
 * @name module:util.range
 * @function
 * @public
 * @param {Number} [start=0] - the value of start
 * @param {Number} stop - the value of stop
 * @param {Number} [step=1] - the value of step
 * @returns {Number[]} the range as an array of numbers
 */
Create a sequence of integers.
The sequence is such that the integer at index i is: start + step * i, with i >= 0 and start + step * i < stop

 Note: this is a JavaScript implement of the Python range function, which explains the unusual management of arguments.


Create a sequence of integers . The sequence is such that the integer at index i is : start + step * i with i > = 0 and start + step * i < stop Note : this is a JavaScript implement of the Python range function which explains the unusual management of arguments .
function range(...args)
{
	const response = {
		origin: "util.range",
		context: "when building a range of numbers",
	};
	try
	{
		let start, stop, step;
		switch (args.length)
		{
			case 0:
				throw "at least one argument is required";
			case 1:
				start = 0;
				stop = args[0];
				step = 1;
				break;
			case 2:
				start = args[0];
				stop = args[1];
				step = 1;
				break;
			case 3:
				start = args[0];
				stop = args[1];
				step = args[2];
				break;
			default:
				throw "range requires at least one and at most 3 arguments";
		}
		if (!Number.isInteger(start))
		{
			throw "start should be an integer";
		}
		if (!Number.isInteger(stop))
		{
			throw "stop should be an integer";
		}
		if (!Number.isInteger(step))
		{
			throw "step should be an integer";
		}
		if (start >= stop)
		{
			return [];
		}
		let result = [];
		for (let i = start; i < stop; i += step)
		{
			result.push(i);
		}
		return result;
	}
	catch (error)
	{
		throw { ...response, error };
	}
}
function range ( ... args ) { const response = { origin : "util.range" , context : "when building a range of numbers" , } ; try { let start , stop , step ; switch ( args . length ) { case 0 : throw "at least one argument is required" ; case 1 : start = 0 ; stop = args [ 0 ] ; step = 1 ; break ; case 2 : start = args [ 0 ] ; stop = args [ 1 ] ; step = 1 ; break ; case 3 : start = args [ 0 ] ; stop = args [ 1 ] ; step = args [ 2 ] ; break ; default : throw "range requires at least one and at most 3 arguments" ; } if ( ! Number . isInteger ( start ) ) { throw "start should be an integer" ; } if ( ! Number . isInteger ( stop ) ) { throw "stop should be an integer" ; } if ( ! Number . isInteger ( step ) ) { throw "step should be an integer" ; } if ( start >= stop ) { return [ ] ; } let result = [ ] ; for ( let i = start ; i < stop ; i += step ) { result . push ( i ) ; } return result ; } catch ( error ) { throw { ... response , error } ; } }
Create a sequence of integers.
Create a sequence of integers .
// 1 arg: start = 0, stop = arg, step = 1 // 2 args: start = arg1, stop = arg2 // 3 args: // if start >= stop, the range is empty: