Use PHP functions in JavaScript

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;}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Rocket
5 May '11 Permalink

q  I found an error with this code.

range('1', '10');



This makes Google Chrome lock up.

Lines 21 and 22 should be:

inival = parseInt(low, 10);
endval = parseInt(high, 10);

Gravatar
Brett Zamir
17 Mar '11 Permalink

q  @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.)

Gravatar
George
16 Mar '11 Permalink

q  I apologize. The code that you can use from jPaq is this:

Array.range(97,106).map(function(num) {
  return String.fromCharCode(num);
})

Gravatar
George
16 Mar '11 Permalink

q  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/

Gravatar
Kevin van Zonneveld
14 Dec '09 Permalink

q  @ cfddream: Thanks for sharing. Does your function also support alphanumeric ranges like in the 3rd example though?

Gravatar
cfddream
6 Dec '09 Permalink

q  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]


Contribute a New function