Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!