Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables The Variable Challenge Solution

Jim Andrews
Jim Andrews
3,110 Points

Used += instead of inline concatenation to make my "story" variable print the whole thing. Good or bad?

So instead of using the inline method of concatenation, I just kept updating my "story" variable, then printed that to the screen so my document.write() could be short and I could avoid this big long line of text to search through for potential errors. Thoughts? Right or wrong? Will this method cause the browser to work slower (I guess this is the real question)?

var noun = prompt("Give me a noun.");
var verb = prompt("Give me a verb.");
var adjective = prompt("Give me a adjective.");
var story = "";

story += "Once upon a time there was a great big ";
story += noun;
story += ". The ";
story += noun;
story += " would ";
story += verb;
story += " very ";
story += adjective; 
story += ". Thanks for reading!!!";


1 Answer

Steven Parker
Steven Parker
216,895 Points

This method is essentially equivalent to what you started with, and as you point out can make the code easier to read and maintain. You might also consider a mix of the two, assembling a sentence or phrase at a time, but not so much to make the line overly long.

Good job, applying your new skills creatively! :+1: