Implementing a pythonic range function in javascript - Node.js Number

Node.js examples for Number:Range

Description

Implementing a pythonic range function in javascript

Demo Code

// from a comment under http://yesudeep.wordpress.com/2009/07/25/implementing-a-pythonic-range-function-in-javascript-2/
function range() {
    // Similar to the python range() function
    var L = arguments, start, stop, step, r = []
    if (L.length == 1) {
        start = 0, stop = L[0], step = 1
    }/*from w w  w .j av  a 2  s  .  c  o m*/
    else {
        start = L[0], stop = L[1], step = L[2] == null ? 1 : L[2]
    }
    for (var i = start; step > 0 ? i < stop : i > stop; i += step) {
        r.push(i)
    }
    return r
}

Related Tutorials