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

Need help using loops

Can somebody tell me what is wrong with this code? It doesn't seem to be running.

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

1 Answer

Your code is perfectly fine and should run.

Most probably you forgot to tell your index.html to use the JavaScript file.

Did you place the following code right above your closing body tag?

<script type="text/javascript" src="script.js"></script>
</body>

content of script.js:

function getRandomNumber(upper) { 
return Math.floor( Math.random() * upper ) + 1; }

var counter = 0;

while ( counter < 10 ) { 
var randNum = getRandomNumber(6); 
document.write(randNum + " "); 
counter += 1; 
}

Like this?

Yep. You were right. Thanks for the help