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
Fredrik Johansson
Full Stack JavaScript Techdegree Student 12,390 PointsMy loop isn't looping. Trying to add 10 random numbers to an array.
Seem's like my code works but it won't loop the process 10 times as intended. Instead it just does this once. Any ideas?
function random100() {
return Math.floor(Math.random() * 100) + 1;
}
function createRandomList(){
for (var i = 0; i < 10; i +=1){
var random = random100();
var box = [];
box.push(random);
}
return box;
}
createRandomList();
6 Answers
Scott Laughead
12,750 PointsIt's looping correctly, your problem is you are declaring the box variable inside of the loop. Therefore, every time the loop runs you are overwriting the preexisting box variable and replacing its contents with a new random number.
Fredrik Johansson
Full Stack JavaScript Techdegree Student 12,390 PointsOh of course! Been looking at the code for almost an hour... Thanks for the clarification!
Manjila Nakarmi
7,449 PointsYou need to declare the variable before you start looping, then it’ll work. As Scott said, you’re redevlaring the array as an empty array everytime you loop right now
Gari Merrifield
9,598 PointsScott Laughead has the primary issue answered, you are re-initializing your box array variable INSIDE the loop, rather than before the loop, meaning you are throwing away any previously stored data. Initialize your varibale before the 'for loop', not inside of it. It appears that you are also not capturing the returned result when you call the "createRandomList();", you need to be assigning the result to a variable if you actually want to make use of the list at some later point.
Fredrik Johansson
Full Stack JavaScript Techdegree Student 12,390 PointsRight. Added the function to a variable. Currently trying to solve another loop which will contain that variable and trying to access each element within the array. Not good at all with loops and arrays.
Gabriel Kroll
9,823 PointsI recommend you to keep it simple.
You want to generade a random number and use a loop to push this number to your box array. Isn’t it?
Thank you for the good challenge. I tried to solve it myself. Now I’m going to use the map function to filter all numbers in the box array that are above 5. Let’s see how this goes. :)
Gabriel Kroll
9,823 PointsI’ll filter all numbers above 50. It makes more sense, since I rarely get a number under 5.
Gabriel Kroll
9,823 PointsTo be fair, I have to admit that I didn’t wrap the loop, that pushes the numbers into the box, into a function.
I named the first function “generateRandomNumber”. That is what your part with Math.random() does.
Then I just took the for loop to push the generated numbers into the box.
But maybe wrapping the loop is what you want to do. Putting the for-loop into a new function that you can call over and over again.
Should this function wrapping the for-loop add more random numbers to the box array every time you call it?
I’m just curious to understand the goal you set yourself with the code you write.
Gabriel Kroll
9,823 PointsAnyways. We can compare our code if you want. Just let me know.
Gari Merrifield
9,598 PointsGari Merrifield
9,598 PointsYou'll get it. It just takes some practice with loops and arrays. Been coding for over 40 years, and I still run into some oddities from time to time. And of course, most languages these days are a moving target, new functions being added, old ones being deprecated, have to keep the documentation links in favorites ;o)