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) Storing and Tracking Information with Variables The Variable Challenge

I would like to break my document.write object string in several lines.

Why "\n" does not break line?

Code:

var name = prompt("Hej, what's your name?"); var start = prompt("Would you like to play a game?"); var feel = prompt('How do you feel today?'); var place = prompt("Where would you like to be today?"); var blocker = prompt("What is holding you back?");

alert("ok, I would like to say something.");

var message = "Chillax " + name + "!"+ "\n" + " There is nothing special about to feeling " + feel + "." + "\n" + " Speaking about" + '"' + place + '"' + " it's just direction." + "\n" + " You are the only one person who can impact " + "'" + blocker + "'" + " as others may not realize that " + "'" + blocker + "'" + " may be slowing you down." + "\n" + start + ", everything is possible!";

document.write (message);

How I could paste code that it would look great, same as in workspace or in IDE?

1 Answer

Hi Martins,

You are rendering to HTML so you need to give the HTML line break to work. I's <br> instead of the javascript \n character. So try it like this and it will work:

var name = prompt("Hej, what's your name?"); var start = prompt("Would you like to play a game?"); var feel = prompt('How do you feel today?'); var place = prompt("Where would you like to be today?"); var blocker = prompt("What is holding you back?");

alert("ok, I would like to say something.");

var message = "Chillax " + name + "!"+ "<br>" + " There is nothing special about to feeling " + feel + "." + "<br>" + " Speaking about" + '"' + place + '"' + " it's just direction." + "<br>" + " You are the only one person who can impact " + "'" + blocker + "'" + " as others may not realize that " + "'" + blocker + "'" + " may be slowing you down." + "<br>" + start + ", everything is possible!";

document.write (message);

Edit: If you log to the console you will need the \n character since it's not HTML that you are rendering. Also, take a look at the cheatsheet underneath your post for a reference on how to post code. You need 3 backticks (`) followed by the name of your programming language to open code. 3 backticks will close it again.

Thank you Elian.