Set a range of numbers as a variable (Javascript)

20,257

Solution 1

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.

Solution 2

Store the minimum and maximum range in an object:

var a = {
    from: 0,
    to: 100
};

Solution 3

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]

Solution 4

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
}

Solution 5

Supported in all modern browsers including IE9+.

var numbers = Array.apply(null,Array(100)).map(function(e,i){return i+1;});
Share:
20,257
Johnny
Author by

Johnny

Aspiring coder.

Updated on February 08, 2021

Comments

  • Johnny
    Johnny about 3 years

    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.

    • Filipe
      Filipe over 8 years
      Not possible in Javascript.
    • rgajrawala
      rgajrawala over 8 years
      @Filipe Really? I can think of many different ways to do it.
    • Filipe
      Filipe over 8 years
      Yea, there is workarounds, but you can't do natively like in PHP
    • zero298
      zero298 over 8 years
      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.
    • MaxZoom
      MaxZoom over 8 years
      Check this implementation
  • dewd
    dewd over 8 years
    shame IE doesn't support fill. enjoying getting used to dropping all the IE8 restrictions to do cool things like this.
  • Admin
    Admin over 8 years
    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
  • Selenir
    Selenir over 8 years
    I would recommend using for loop in this case, it's much faster: jsperf.com/range5254535
  • rgajrawala
    rgajrawala over 8 years
    @dewd You can use the polyfill.