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!

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 A Closer Look at Loop Conditions

Javascript not printing

my document write dont print. pleaase help and elaborate on why it don't.

thanks

var upper = 10000; var randomNumber = getRandomNumber(upper); var guess; var attempts = 0;

function getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; } while(guess !== randomNumber){ guess = getRandomNumber(upper); attempts +=1;

} document.write("<p>The random number was " + randomNumber <p>"); document.write("<p>The computer guessed "+ attempts + "to get it right</p>");

4 Answers

Your variants are listed as:

 var randomNumber = getRandomNumber(upper)
 var attempts = 0;

The document will not write because you have listed the variants with () behind them.

 document.write("<p>The random number was " + randomNumber() + "<p>"); 
 document.write("<p>The computer guessed " + attempts()+ "to get it right</p>");

Try this:

 document.write("<p>The random number was: " + randomNumber + "</p>");
 document.write("<p>The computer guessed " + attempts + " to get it right.</p>");

Good luck!

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

This part of your code needs correction with the quaote mark:

 + randomNumber <p>"

you need a " on left side of p & you need () around randomNumber and a + between these. I will give you a chance to try this out.

document.write("<p>The random number was " + (randomNumber) "<p>"); document.write("<p>The computer guessed "+ (attempts) + "to get it right"</p>");

no difference

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

You still have it wrong. Try this:

document.write("<p>The random number was " + randomNumber() + "<p>"); 
document.write("<p>The computer guessed "+ attempts+ "to get it right</p>");

A function is defined like this: function()

var upper = 10000; var randomNumber = getRandomNumber(upper); var guess; var attempts = 0;

function getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; } while(guess !== randomNumber){ guess = getRandomNumber(upper); attempts += 1;

}

document.write("<p>The random number was " + randomNumber() + "<p>"); document.write("<p>The computer guessed " + attempts()+ "to get it right</p>");

even your code dont work so?????? unless I'm a complete idiot (my guess is I am) there is something wrong with workspaces, right?

agusruiz
agusruiz
9,668 Points

Hello Ben, you had it almost right the first time, you only had some typos, you were not closing the strings correctly.

Original HTML

document.write("<p>The random number was " + randomNumber <p>");
document.write("<p>The computer guessed "+ attempts + "to get it right</p>");

Corrected HTML

document.write("<p>The random number was " + randomNumber + "</p>");
document.write("<p>The computer guessed " + attempts + " to get it right.</p>");

Now on the last comment you posted, the only thing to fix is that both randomNumber and attempts are not functions, they are variables, so you dont need to use (). Remove that and your code should work, I tried using you code removing that and it worked.

Final code

var upper = 10000; 
var randomNumber = getRandomNumber(upper); 
var guess; 
var attempts = 0;

function getRandomNumber(upper) { 
  return Math.floor( Math.random() * upper ) + 1; 
} 
while(guess !== randomNumber){ 
  guess = getRandomNumber(upper); attempts += 1;
}

document.write("<p>The random number was " + randomNumber + "<p>"); 
document.write("<p>The computer guessed " + attempts+ "to get it right</p>");

P.S. I used google chrome developer tools to debug your code.

Hope this helps, coding sometimes gets a little bit frustrating, but it is so cool to see something once you are done. Keep it up!