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?

Susan Rusie
Susan Rusie
10,671 Points

Not sure why my code isn't showing the random numbers

I just finished watching this video and have gone over and over this code. I don't know why my code is only showing the title but no random numbers. I have even tried refreshing the browser to see if that made a difference, but it hasn't. What is missing that would fix this problem.

Susan Rusie
Susan Rusie
10,671 Points

Here is my code:

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

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Let’s Make Random Numbers</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Let’s Make Random Numbers</h1> <script src="scripts.js"></script> </body> </html>

2 Answers

Steven Parker
Steven Parker
210,871 Points

You forgot to blockquote your code, so some of it is not visible. To quote your code, skip a line, put a line with only three accents ("backticks") and the language id, then the code, then another line with just 3 accents, like this:

```js

(code goes here)

```

But I took a guess that your code was originally like this:

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

This code ran fine and produced 10 random numbers between 1 and 6. But no title - and I don't see anything in the code that would create a title.

Susan Rusie
Susan Rusie
10,671 Points

Sorry about that. I still can't get it to show the random numbers, just " Let's Make Random Numbers". Here is my code:

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

The while condition is incomplete, and the open brace for the block is missing (same problems as the unquoted version).

If you look at my version, you'll see where I guessed it was intended to test for 10 iterations. With those fixes applied, it worked fine.

while (counter < 10) {

What is displaying "Let's Make Random Numbers"? Certainly nothing in this code. Is there an HTML part that goes with this? Is this from a workspace that you could snapshot and share a link to?

Susan Rusie
Susan Rusie
10,671 Points

I wasn't sure how to post the HTML, but it's fixed now. Thanks again for your help.

Susan, just to mention, this isn't HTML, this is the programming language JavaScript. I know it can get confusing considering all three languages: HTML, CSS, and JavaScript all work together to create a web page. If it would help to have a relational, if you know anything about Japanese, you can't just translate Japanese into English. You have to use three different forms of Japanese: hiragana, katakana, and kanji to translate. They're not the same as each other, but they're all needed to create the whole process of translating from one language to the other. This is the same for JavaScript: it's part of a three step process of languages to make a web page.