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 trialMartins Tikmanis
1,616 PointsI 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);
1 Answer
elk6
22,916 PointsHi 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.
Martins Tikmanis
1,616 PointsThank you Elian.
Martins Tikmanis
1,616 PointsMartins Tikmanis
1,616 PointsHow I could paste code that it would look great, same as in workspace or in IDE?