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: 909.322
    // 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
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