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 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,407 Points

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

Matthew Long
Matthew Long
28,407 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 !