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!

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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

Why am I only getting 5 random numbers instead of 10?

I cant see anything wrong with my code, yet when I preview my workspace, its only giving me 5 random numbers, not 10. And when I changed 10 to 100, it only gave me about 40.

function randomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; } var counter = 0; while(counter < 10) { counter += 1;
var randNum = randomNumber(6); document.write(randNum + ' ' ); counter+=1; }

I can't figure out what I'm missing.

2 Answers

You increment counter twice in the while loop. So the first time through, counter becomes 2 (0 + 1 + 1). The second time, counter becomes 4, etc. After 5 times counter is 10 so the loop stops running.

Oh geeze. I must have looked over this at least 10 times and I didnt see that. Thank you so much!