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

MaryJo Foster
MaryJo Foster
2,601 Points

I'd like to have newlines/returns at the end of some strings in my story. How do I add returns to text strings

I've tried "\r", "\n", and "\b" to no success. Any suggestions?

Neil Docherty
Neil Docherty
10,418 Points

Can you show us your code?

3 Answers

Diego Nunes
Diego Nunes
9,478 Points

You're using document.write to print as html, so use "< br / >" without spaces instead of "\n" You can wrap your phrase with a paragraph tag too, give a title using a h1 and so on...

var userNoun = prompt("To being our story, please enter a noun (person, place, or thing)");
var userVerb = prompt("Next, we need a verb for an action phrase!");
var userAdj = prompt("Next, we need an adjective to describe the verb!");
alert("Excellent choices, your part is done now! Let's see what story we can make of this!"); 
var phraseStart_End = ("<br />* * * * * * * * * * * * * * * * * * * * * * * * *<br />");
var phraseNoun = "Once upon a time, " + userNoun + " ";
var phraseVerb = userVerb.toLowerCase() + " with MJ. ";
var phraseAdj = "Most of the time they " + userVerb.toLowerCase() + " " + userAdj.toLowerCase() + " together, but not always!."; 
document.write(phraseStart_End);
document.write(phraseNoun + phraseVerb + phraseAdj);
document.write(phraseStart_End);
Neil Docherty
Neil Docherty
10,418 Points

If you open up your JavaScript console on your web-browser and type in the following two lines of code you should see that \n does indeed work.

var foo="Hello \nWorld";
alert(foo);
MaryJo Foster
MaryJo Foster
2,601 Points

Thanks Neil,

I tried what you suggested, but still no success. Here's my code ...

var userNoun = prompt("To being our story, please enter a noun (person, place, or thing)"); var userVerb = prompt("Next, we need a verb for an action phrase!"); var userAdj = prompt("Next, we need an adjective to describe the verb!"); alert("Excellent choices, your part is done now! Let's see what story we can make of this!"); var phraseStart_End = ("\n* * * * * * * * * * * * * * * * * * * * * * * * *\n"); var phraseNoun = "Once upon a time, " + userNoun + " "; var phraseVerb = userVerb.toLowerCase() + " with MJ. "; var phraseAdj = "Most of the time they " + userVerb.toLowerCase() + " " + userAdj.toLowerCase() + " together, but not always!."; document.write(phraseStart_End); document.write(phraseNoun + phraseVerb + phraseAdj); document.write(phraseStart_End);

Below is the output, still without newlines:

The Story Maker

  • * * * * * * * * * * * * * * * * * * * * * * * * Once upon a time, Steve ran with MJ. Most of the time they ran quickly together, but not always!. * * * * * * * * * * * * * * * * * * * * * * * * *

Any other suggestions anyone?

MaryJo Foster
MaryJo Foster
2,601 Points

Thanks Diego, this worked! I added the break, WITHOUT space as seen here " < b r / >", and it worked perfectly!


HERE'S MY NEW CODE:

var userNoun = prompt("To being our story, please enter a noun (person, place, or thing)");

var userVerb = prompt("Next, we need a verb for an action phrase!");

var userAdj = prompt("Next, we need an adjective to describe the verb!");

alert("Excellent choices, your part is done now! Let's see what story we can make of this!");

var phraseStart_End = (" < b r / >* * * * * * * * * * * * * * * * * * * * * * * * * < b r / > ");

var phraseNoun = "Once upon a time, " + userNoun + " ";

var phraseVerb = userVerb.toLowerCase() + " with MJ. < b r / >";

var phraseAdj = "Most of the time they " + userVerb.toLowerCase() + " " + userAdj.toLowerCase() + " together, but not always!.";

document.write(phraseStart_End);

document.write(phraseNoun + phraseVerb + phraseAdj);

document.write(phraseStart_End);


Below is the output as I intended! ....

The Story Maker

<<< Stars are displayed here >>>

Once upon a time, Steve walked with MJ.

Most of the time they walked quickly together, but not always!.

<<< Stars are displayed here >>>