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

Nicholette Taylor
Nicholette Taylor
2,832 Points

Fixed it! Just sharing.

The large number of spaces (carriage returns) that I had put inside the string to format the final paragraph were breaking my whole story and stopping the prompts from loading. It took long to figure it out on my own, but I finally just wrote each line of code (and hit save after each command) like we were instructed to and it made it easier to find the problems.

Once I deleted all of the carriage returns and extra spaces (I had put returns inside the string which doesn't work) things worked well. Corrected code: var finStory = ("In my " + adjective + " garden, the " + vegetable + " will be planted on the left and the " +fruit+ " will be planted on the right and the " + herb + " will be planted at the front to soak up all of the sun. I will " + verb + " my garden.");

Any comments on how to properly format the "var finStory" string so it looks like a nice paragraph instead of a long line of text would be appreciated. thanks.

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Check out MDN Text Formatting for some good info...

Two options are to use escape characters and/or template literals.

Here's two examples, 1 escaping the newline with \n and the second with using the template literal for you to experiment with:

let adjective = 'silly';
let vegetable = 'spinach';
let fruit = 'mango';
let herb = 'thyme';
let verb = 'enjoy';


let finStory = (
    "In my "
    + adjective
    + ' garden, \nthe '
    + vegetable
    + " will be planted on the left \nand the "
    + fruit
    + " will be planted on the right \nand the "
    + herb
    + " will be planted at the front \nto soak up all of the sun. \nI will "
    + verb
    + " my garden.");

console.log('\nEscape new line\n' + finStory);

let finStory2 = (`In my ${adjective} garden,
the ${vegetable} will be planted on the left
and the ${fruit} will be planted on the right
and the ${herb} will be planted at the front
to soak up all of the sun.
I will ${verb} my garden.`
);

console.log('\nTemplate literal\n' + finStory2);
// Outputs
Escape new line
In my silly garden, 
the spinach will be planted on the left 
and the mango will be planted on the right 
and the thyme will be planted at the front 
to soak up all of the sun. 
I will enjoy my garden.

Template literal
In my silly garden,
the spinach will be planted on the left
and the mango will be planted on the right
and the thyme will be planted at the front
to soak up all of the sun.
I will enjoy my garden.