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 Basics (Retired) Making Decisions with Conditional Statements Build a Random Number Guessing Game

line 6 document.write issues

Hello to whoever is reading this!

Below is my code. Lines 1-5 all work as I tested each part separately, but for some reason, my final document.write on line 6 will only write the string " ' + randomNumber + ' ". The document.write picks up on everything else properly, but not that particular part. I've been at this for awhile now, so my eyes are probably missing something very simple. Any help is greatly appreciated!

var randomNumber = Math.floor(Math.random() * 6) + 1;
var guess = prompt("I am thinking of a number between 1 and 6. What is it?");
if (parseInt(guess) === randomNumber) {
  document.write("<p>You are right!</p>");
} else {
  document.write("<p>WRONG! The correct number was ' + randomNumber + ' </p>");
}

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Blake,

Careful with your quotation marks! If you're opening with doubles, you have to close with doubles and vice-versa:

var randomNumber = Math.floor(Math.random() * 6) + 1;
var guess = prompt("I am thinking of a number between 1 and 6. What is it?");
if (parseInt(guess) === randomNumber) {
  document.write("<p>You are right!</p>");
} else {
  document.write("<p>WRONG! The correct number was " + randomNumber + " </p>");
}

Ahhhhh! I was under the impression that if I started with double and I intended to use another set of quotes following I would need to use the alternate, the single. Thank you for correcting my mistake!

Greg Kaleka
Greg Kaleka
39,021 Points

Sure thing! There are times when you'd want to use both. For example, if you were outputting html with quotes in it, you'd want to do something like this:

document.write('<p class="red-text">WRONG! The correct number was ' + randomNumber + '</p>');