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

Aaron Coursolle
Aaron Coursolle
18,014 Points

Still having trouble wrapping mind around JS shorthand.

Here is my code for the short story challenge. While this longhand version of code works, I had difficulty with the earlier lesson on shorthand (using +=) and maybe if someone provided an example based on something that I've wrote from scratch it will help me understand it better:

alert("We will now write a story.");
alert("For each prompt, give a brand new answer.");
var lady = prompt("Name of a Person or an Animal");
var shoe = prompt("Name something that is hollow.");
var children = prompt("A flock or herd of_______.");
var broth = prompt("What is your favorite drink, when you go to a restaurant?");
var bread = prompt("______ is the most boring food item, ever.");
var whipped = prompt("Give a Past-Tense Verb.");
var bed = prompt("And, finally, where is your favorite place?");
alert("Thank you for playing.  Click OK to see your result");

document.write("There was an old " + lady + " who lived in a " + shoe + ". She had so many " 

+ children + ", she didn't know what to do;  She gave them some " + broth + " without any " 

+ bread + "; Then " + whipped + " them all soundly and sent them to " + bed + ".");

thanks.

Sean T. Unwin
Sean T. Unwin
28,690 Points

I fixed the formatting for you. There has to be an empty return (blank newline) before the ticks. :)

3 Answers

Piotrek Smyda
Piotrek Smyda
3,786 Points

Not nice but I think you will see it here:

var text = "";

alert("We will now write a story."); 
alert("For each prompt, give a brand new answer."); 

var lady = prompt("Name of a Person or an Animal"); 
text += "There was an old " + lady ;

var shoe = prompt("Name something that is hollow."); 
text += " who lived in a " + shoe;

var children = prompt("A flock or herd of____."); 
text += ". She had so many " + children;

var broth = prompt("What is your favorite drink, when you go to a restaurant?"); 
text += ", she didn't know what to do; She gave them some " + broth;

var bread = prompt("___ is the most boring food item, ever."); 
text += " without any " + bread;

var whipped = prompt("Give a Past-Tense Verb."); 
text += "; Then " + whipped;

var bed = prompt("And, finally, where is your favorite place?"); 
text += " them all soundly and sent them to " + bed + ".";

alert("Thank you for playing. Click OK to see your result");


document.write(text);

Hope this will clear your head :)

Hey Aaron,

The += operator is an operator that takes what was previously stored in the variable and adds new data to it. In the case of strings, it appends the new string(s) to the end of what is there already. In the case of numbers, it adds the number that was already in the variable to the new number.

Let's say, for example, that you have a variable:

var story = 'The story goes...';

You know that story of course equals "The story goes..." right now. So, if I do another line, that adds "Once upon a time, there was a frog.", I can use the += operator to add "The story goes..." to "Once upon a time, there was a frog." so that it will read "The story goes...Once upon a time, there was a frog."

var story = 'The story goes...';
story += 'Once upon a time, there was a frog.';
//See what value story is in console
console.log(story);
//Will output "The story goes...Once upon a time, there was a frog."

So, what it did was take what was previously in story and add in some new data. Each time you use += it's going to take what was already there and then add the new data to the end.

Does that make more sense?

You've already had some solid responses but I wanted to give you one more example that most closely resembles your code:

alert("We will now write a story.");
alert("For each prompt, give a brand new answer.");
var lady = prompt("Name of a Person or an Animal");
var shoe = prompt("Name something that is hollow.");
var children = prompt("A flock or herd of_______.");
var broth = prompt("What is your favorite drink, when you go to a restaurant?");
var bread = prompt("______ is the most boring food item, ever.");
var whipped = prompt("Give a Past-Tense Verb.");
var bed = prompt("And, finally, where is your favorite place?");

alert("Thank you for playing.  Click OK to see your result");

var final = "There was an old " + lady + " who lived in a " + shoe;
      final += ". She had so many " + children + ", she didn't know what to do;";
      final += " She gave them some " + broth + " without any " + bread;
      final += "; Then " + whipped + " them all soundly and sent them to " + bed + ".";

document.write(final);

I think this improves readability but it is a personal choice. I hope this helps.