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

document.write("<p> Sorry. The number was" + randomNumber + </p>") Why is the last (+) sign before the closing <p>tag

document.write("<p> Sorry. The number was" + randomNumber + </p>") Why is the last (+) sign before the closing <p> tag necessary?

2 Answers

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Nicollette,

Posting the line of code Dave uses in the video for reference:

document.write('<p> Sorry. The number was ' + randomNumber + '</p>');

Now for the explanation.

If you removed the + sign before the closing paragraph tag, the code would crash since you would be telling the browser to write out "Sorry. The number was ", then it would hit the plus sign telling it to append the value of randomNumber to the end of the line, then it wouldn't know what you wanted to do with the closing paragraph in quotations since there isn't an operator telling it to concatenate it to the end of the line.

The plus sign simply tells the browser to add it to the end of the line.

A shorter explanation would be that the browser would understand everything up until it added the value of randomNumber to the end of the line, then it would expect a semi-colon to end the declaration. Instead, it gets more code and doesn't know how to process it.

Good luck with the course!

Justin Eaton
Justin Eaton
6,133 Points

It helps to remember that you are telling the javascript to write to the html file. Since your string starts with a <p> tag it needs the closing </p> tag and unless you include the + and wrap the closing </p> tag in quotes like this + "</p>"); or this + '</p>'); your html will be missing the closing tag it needs.