JavaScript range
Create an array containing the range of integers or characters from low to high (inclusive)
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 | function range (low, high, step) { // Create an array containing the range of integers or characters from low to high (inclusive) // // version: 1109.2015 // discuss at: http://phpjs.org/functions/range // + original by: Waldo Malqui Silva // * example 1: range ( 0, 12 ); // * returns 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] // * example 2: range( 0, 100, 10 ); // * returns 2: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] // * example 3: range( 'a', 'i' ); // * returns 3: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] // * example 4: range( 'c', 'a' ); // * returns 4: ['c', 'b', 'a'] var matrix = []; var inival, endval, plus; var walker = step || 1; var chars = false; if (!isNaN(low) && !isNaN(high)) { inival = low; endval = high; } else if (isNaN(low) && isNaN(high)) { chars = true; inival = low.charCodeAt(0); endval = high.charCodeAt(0); } else { inival = (isNaN(low) ? 0 : low); endval = (isNaN(high) ? 0 : high); } plus = ((inival > endval) ? false : true); if (plus) { while (inival <= endval) { matrix.push(((chars) ? String.fromCharCode(inival) : inival)); inival += walker; } } else { while (inival >= endval) { matrix.push(((chars) ? String.fromCharCode(inival) : inival)); inival -= walker; } } return matrix;} |
Examples
» Example 1
Running
1 | range ( 0, 12 ); |
Should return
1 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] |
» Example 2
Running
1 | range( 0, 100, 10 ); |
Should return
1 | [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have range goodness in JavaScript.
@George. You can also compile only the functions you need for php.js. Personally speaking, doing "range('a', 'i');" seems easier and clearer than writing multi-stepped non-semantic code, even if that code is in a nice functional style. (It would be nice, I'll admit if our compiler could allow chaining in array functions, possibly stripping of the redundant "array_" prefix as in the PHP API, but in this case the php.js way seems easier.)
I apologize. The code that you can use from jPaq is this:
Array.range(97,106).map(function(num) {
return String.fromCharCode(num);
})
I just downloaded a build of jPaq that only contains the array functions. I definitely like how easy dealing with arrays can be. I used the following code to produce the lower-case letters a through i.
Array.range(97,106).forEach(function(num) {
return String.fromCharCode(num);
})
I can also easily emulate the other examples as well with jPaq. FYI, if you are looking for a neat javascript library that you custom build, make it at http://www.jpaq.org/
@ cfddream: Thanks for sharing. Does your function also support alphanumeric ranges like in the 3rd example though?
This is my 'range' function:
function range(start, end, step){
var l = arguments.length;
if(l == 0) return [];
if(l == 1) return arguments.callee(0, start, 1);
if(l == 2) return arguments.callee(start, end, 1);
var temp = []
start = start>>0, end = end>>0, step = step>>0;
//console.log(start, end, step);
for(;start < end; start+= step){
temp.push(start);
}
return temp;
}
range(); // []
range(10); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(-10); // []
range(-10, -20); //[]
range(0, 10); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range(-10, 10, 2); // [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8]


Rocket
5 May '11
range('1', '10');This makes Google Chrome lock up.
Lines 21 and 22 should be: