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 Solution

Jordan Faulds
seal-mask
.a{fill-rule:evenodd;}techdegree
Jordan Faulds
Full Stack JavaScript Techdegree Student 2,315 Points

Creating a var sentence vs. putting all text in the document.write field

Hi there, I successfully completed this challenge (and even monkeyed with the CSS file a tiny bit), but I did it a bit differently than the video above. I found my code easier to read and write when I just put everything I wanted to appear in the story inside the document.write field instead of creating variables for each sentence and then using "sentence +=".

My question is this: is there a reason it's better to use "sentence +="? Or is it just a matter of personal preference?

So you can see what I mean, here's the snapshot of my code: https://w.trhou.se/56nqrz5m8v

Thank you!

2 Answers

kevin curtis
kevin curtis
15,287 Points

Hi Jordan,

It's best practice to do it the way it was suggested. One reason being code readability. Another reason being that when you put your code into production you never want to use the document.write function. Here's a thread you can have a read through if you like: https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice

Victor Warner
Victor Warner
1,882 Points

very interesting I put most of mine in the sentence var with thre +++