0

I have a number for example 10. I want to explode it sequentially in order from 1 until 10, e.g 1,2,3,4,5,6,7,8,9,0

javascript var number = 10 var array = [1,2,3,4,5,6,7,8,9,10] // How to convert the number into this?

rolodex
  • 558
  • 1
  • 7
  • 19

2 Answers2

5
console.log(Array.apply(null, {length:11}).map(Number.call, Number).slice(1));

Output

[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

Original idea from Creating range in JavaScript - strange syntax and it has an awesome explanation of how this answer really works. Its a must read.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

Lets do a quite pseudo code here and you can try it like this:

Create Array [size], consider the size to be the number entered like 10 for example;

Create a loop FOR or WHILE

  • Inside do following remainder = number (10) - 1
  • Array [i] = remainder

Until number (10) reaches 0... I'm sure you can do this by your self.

I am Cavic
  • 1,115
  • 1
  • 10
  • 22