4

Let's say I want a variable to contain numbers from 1 to 100. I could do it like this:

var numbers = [1,2,3,4,...,98,99,100]

But it would take a bunch of time to write all those numbers down. Is there any way to set a range to that variable? Something like:

var numbers = [from 1, to 100] 

This might sound like a really nooby question but haven't been able to figure it out myself. Thanks in advance.

Johnny
  • 149
  • 1
  • 3
  • 12
  • Not possible in Javascript. – Filipe Jan 25 '16 at 23:36
  • 3
    @Filipe Really? I can think of many different ways to do it. – rgajrawala Jan 25 '16 at 23:36
  • 2
    [Yea](http://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-an-array-based-on-suppl), there is workarounds, but you can't do natively like in [PHP](http://php.net/manual/pt_BR/function.range.php) – Filipe Jan 25 '16 at 23:37
  • Does it have to be an array? The only thing that I can think of similar to this would be an object `{from: 0, to: 100}`. That fits in a variable, but it is a constructed object that you would have to use by convention. JavaScript primitives can't really do this. – zero298 Jan 25 '16 at 23:38
  • Check this [implementation](https://github.com/jashkenas/underscore/blob/3699e39631cfaa99ca88dc279decb6ff57989413/underscore.js#L691) – MaxZoom Jan 25 '16 at 23:46

6 Answers6

6

Store the minimum and maximum range in an object:

var a = {
    from: 0,
    to: 100
};
zero298
  • 25,467
  • 10
  • 75
  • 100
4

In addition to this answer, here are some ways to do it:

for loop:

var numbers = []
for (var i = 1; i <= 100; i++) {
    numbers.push(i)
}


Array.prototype.fill + Array.prototype.map

var numbers = Array(100).fill().map(function(v, i) { return i + 1; })

Or, if you are allowed to use arrow functions:

var numbers = Array(100).fill().map((v, i) => i + 1)

Or, if you are allowed to use the spread operator:

var numbers = [...Array(100)].map((v, i) => i + 1)


However, note that using the for loop is the fastest.

Community
  • 1
  • 1
rgajrawala
  • 2,148
  • 1
  • 22
  • 35
  • shame IE doesn't support fill. enjoying getting used to dropping all the IE8 restrictions to do cool things like this. – dewd Jan 25 '16 at 23:54
  • 1
    You're trying to map using the index, but index is second param of map. Add v as value placeholder as first arg - current implementation with map returns an array of 100 NaNs –  Jan 26 '16 at 00:14
  • 1
    I would recommend using `for` loop in this case, it's much faster: http://jsperf.com/range5254535 – Selenir Jan 26 '16 at 00:22
  • @dewd You can use [the polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#Polyfill). – rgajrawala Jan 26 '16 at 17:48
1

Supported in all modern browsers including IE9+.

var numbers = Array.apply(null,Array(100)).map(function(e,i){return i+1;});
dewd
  • 4,380
  • 3
  • 29
  • 43
1

You can easily create your own, and store only the limits:

function Range(begin, end) {
  this.low = begin;
  this.hi = end;
  this.has = function(n) {
     return this.low <= n <= this.hi;
  }
}

// code example
var range = new Range(1,100);
var num = 5;

if (range.has(num)) {
  alert("Number in range");
}
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
1

For what it's worth, @MaxZoom had answer that worked for my situation. However, I did need to modify the return statement to evaluate with && comparison. Otherwise appeared to return true for any number.

function Range(begin, end) {
  this.low = begin;
  this.hi = end;
  this.has = function(n) {
      //return this.low <= n <= this.hi;
      return ( n >= this.low && n <= this.hi );
  };
}

// code example
var alertRange = new Range(0,100);
var warnRange = new Range(101, 200);

var num = 1000;

if (alertRange.has(num)) {
    alert("Number in ALERT range");
    //Add Alert Class
}
else if (warnRange.has(num)) {
    alert("Number in WARN range");
    //Add Warn Class
}
else{
    alert("Number in GOOD range")
    //Default Class
}
0

Python

# There can be two ways to generate a range in python
# 1) 
a = [*range(5)]
print(a) 
#[0, 1, 2, 3, 4]

# 2) 
a = [*range(5,10)]
print(a) 
#[5, 6, 7, 8, 9]

Javascript

// Similar pattern in Javascript can be achieved by
let a;
// 1)
a = [...Array(5).keys()]
console.log(a) //[0, 1, 2, 3, 4]

// 2) 
a = [...Array(5).keys()].map(value => value + 5);
console.log(a) //[5,6,7,8,9]
Waleed Tariq
  • 825
  • 8
  • 19