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?

Justin Glover
Justin Glover
1,685 Points

Why doesn't this code produce anything?

function randomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; } var counter = 0;

while(counter <= 10){ document.write(randomNumber(5)); counter += 1; }

Justin Glover
Justin Glover
1,685 Points

Nevermind, i got ahead of myself and there was no <script> tag in the HTML file. But now my new question is, why did Dave in his example create a variable randNum? In my example i was able to do so without that variable?

Matthew Long
Matthew Long
28,381 Points

Is your script.js file linked to your index.html using the <script> tag?

Matthew Long
Matthew Long
28,381 Points

You don't have to create a variable and then return it. Like in your example, you can return it right off the bat. Generally you only need to create a variable if you're going to do something with it before you return it. Dave is probably just trying not to throw too much at the viewer at once.

Justin Glover
Justin Glover
1,685 Points

Oh okay, thank you !