range in javascript stackoverflow

Solutions on MaxInterview for range in javascript stackoverflow by the best coders in the world

showing results for - "range in javascript stackoverflow"
Mina
16 Jul 2020
1function range(start, stop, step) {
2    if (typeof stop == 'undefined') {
3        // one param defined
4        stop = start;
5        start = 0;
6    }
7
8    if (typeof step == 'undefined') {
9        step = 1;
10    }
11
12    if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
13        return [];
14    }
15
16    var result = [];
17    for (var i = start; step > 0 ? i < stop : i > stop; i += step) {
18        result.push(i);
19    }
20
21    return result;
22};