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?

what is wrong with my javascript file because every time I try to preview I got nothing except a title on the page?

Let’s Make Random Numbers --- is all i see on top-left corner of the page.

can anyone elaborate what I am missing? Thanks

jag
jag
18,266 Points

Have you checked your browser's console? Other than that without showing your code it's hard to tell.

4 Answers

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

First, please provide your code here

this is my javascript code:

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

and this is my html code:

<!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="video2-script.js"></script> </body> </html>

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

You need to write you while loop on proper way. Like in code below:

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

var counter = 0; 
while ( counter < 10 ){
    var randNum = randomNumber(6);
    document.write(randNum + ' '); 
    counter++; 
}

This code will generate 10 random numbers - https://jsfiddle.net/k53kxof1/

Thanks Sergey, that helpful at the end!

hey Sergey!

Do you know how I can setup my own javascript environment so that I can code along with the instructor as he teaches the lessons?

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

You can use treehouse Workspaces, but I suggest you to write all your code locally and preview it on browser. The best text editor for me is Sulime Text with Emmet plugin

jason chan
jason chan
31,005 Points
var questions = [
  {
    question: 'How many states are in the United States?', 
    answer: 50
  },
  {
    question: 'How many continents are there?',
    answer: 7
  },
  {
    question: 'How many legs does an insect have?', 
    answer: 6
  }
];
var correctAnswers = 0;
var question;
var answer;
var response;

function print(message) {
  document.write(message);
}

for (var i = 0; i < questions.length; i += 1) {
  question = questions[i].question;
  answer = questions[i].answer;
  response = prompt(question);
  response = parseInt(response);
  if (response === answer) {
    correctAnswers += 1;
  } 
}

html = "You got " + correctAnswers + " question(s) right."
print(html);