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

Blake Johnson
Blake Johnson
4,812 Points

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,019 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>");
}
Blake Johnson
Blake Johnson
4,812 Points

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,019 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>');