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!

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

Jonathan Kemper
Jonathan Kemper
5,169 Points

What is the best practice for formulating the story sentences?

In my snapshot here I pieced together my sentences https://w.trhou.se/msi333uzvp.

Based on the videos I interpreted this as being the "best" way to write blocks of concatenated text so that someone reading the code later on could more easily see where hard-coded text ends and string variables begin. However, it seems to be easier to program one long line of code with alternating text and variables. (Like most of the examples posted in the other questions on this video.)

Is one method more commonplace than the other for this sort of scenario in the real world?

2 Answers

Steven Parker
Steven Parker
228,001 Points

I think it's mostly a matter of preference, use the technique that you find easiest to read.

Yet another choice might be to use a template string, which might be even easier to both read and maintain:

var story = `The ${adjective} boy looked out over the rolling mountains and saw a ${noun}.
             He was so excited that he ${verb}.`;
6,173 Points

This was how I did my JavaScript code for this assignment I think there are various ways to accomplish this task and it some may be better than others but I think preference plays a part as well. I hope this helps!

var adj = prompt("Enter an adjective...");
console.log("variable adj is now set to: " + adj);

var verb = prompt("Enter a verb...");
console.log("variable verb is now set to: " + verb);

var noun = prompt("Enter a noun...");
console.log("variable noun is now set to: " + noun);

var story = "There once was a " + adj + " programmer who wanted to use JavaScript to " + verb + " the " + noun


console.log("Program complete");