Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do i make an array have 100 numbers with out me having to write it one by one

https://w.trhou.se/ko1crrpwh2

i would like to know how i can write a function that pushes 100 numbers into an array with out me having to do it manually

3 Answers

Hi,

What kind of numbers are you thinking?

function numbers() {
     //define an empty array
      var array = [];
      for(var i=0; i <= 99; i++) {
             //loop through 0 - 99 and push each index as an integer on the array
            array.push(i);
       }
       //return the array to have access to it outside of the function scope
        return array;
}

//Then you can use the function like this
var nums = numbers();
console.log(nums[0]); //will print 0
console.log(nums[99]); //will print 100

Again this depends on which numbers you want to fill into the array. The above is a common for loop that is used to fill an array with numbers.

Awesome thankyou! i was stuck on the array.length, thats what messed me up