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

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
201,957 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,170 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");