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

Amber Touch
Amber Touch
2,339 Points

My code is exactly like the video, but won't run!

https://w.trhou.se/8aq8dku1vb I'm not sure how to get my snapshot to show up like everyone else's is :S but here is the link anyway. When I hit preview literally nothing happens. It doesn't prompt the user for anything. I really wish I knew why! I checked the console, and I had an error later on in the conditional statement that I fixed (missing a closing quote) but still nothing happens and the console doesn't show any more errors.

4 Answers

Jacob Tollette
Jacob Tollette
27,884 Points

You have an error on line 4.

document.write("<p>You guessed the number!"</p>);

that last "</p>" needs to be within your quotes. Try that.

EDIT:

You have the same type error on line 6 where you're missing the last quote mark after the closing </p> tag.

LINE4 Should be: document.write("<p>You guessed the number!</p>");

LINE6 Should be: document.write("<p>Sorry. The number was " + randomNumber + "</p>");

Simple errors that will drive you crazy.

Jacob Tollette
Jacob Tollette
27,884 Points

I downloaded and tested it like this and it works:

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 guessed the number!</p>");
} else {
  document.write("<p>Sorry. The number was " + randomNumber + "</p>");
}

About separating HTML and JS, you do need to keep them separate. Think of the HTML in your JS like part of a string. What's going on is that the HTML (which is part of the string) is being written to an HTML file as text. That is then being interpreted by the browser. Does that make sense?

Regarding whether the prompt will work? It should work fine. JS is executed top-down. There may be another issue going on with Workspaces or something.

Amber Touch
Amber Touch
2,339 Points

Thanks for your help. It's a little tough at the beginning when you can't figure out what is going wrong. I'll just keep moving forward with the course and not let this snafu sideline my progress. Thanks again!

Jacob Tollette
Jacob Tollette
27,884 Points

That's what I would do. There will be a few more issues along the way. Trust me. Just learn what you need to and move on.

Amber Touch
Amber Touch
2,339 Points

Thank you so very much for your help. I did not notice that issue with the html tags. (I thought we were supposed to separate html and javascript, but I guess not all the time?) That issue on line 4 would stop the alert from going off? Doesn't Javascript run from top to bottom? Seems like the alert should still get executed.

Amber Touch
Amber Touch
2,339 Points

I fixed those two errors, but when I preview it, it still won't execute the prompt (not alert, sorry... I should finish my coffee before posting lol).

Jacob Tollette
Jacob Tollette
27,884 Points

Let me finish MY coffee and I'll take another look. Lol

Patricia Perez
Patricia Perez
Courses Plus Student 880 Points

oh yeah Javascript running from top to bottom and from inside the ( ) to out side the ( )

Javascript Is so easy to use to worry u will get into it so soon , good luck .

When you use Javascript to write HTML, the HTML tags need to be in a string in Javascript.

var str="<p>Hello, world</p>";
document.write(str);

An alternative you may use later on is this:

var p=document.createElement("p");
p.innerHTML="Hello, world";
document.getElementsByTagName("body")[0].appendChild(p);

The latter method is more useful dealing with pages when you don't have write access to the webpage (i.e. adding some functionality to a third-party site you use a lot).