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

Here's my Variable Challenge Solution. It could be simplified. Any thoughts?

var nounPerson = prompt("Please enter the name of a person");
var adjectivePerson = prompt("Please provide a word that describes the person.")
var nounPlace = prompt("Please enter the name of a City, Country or Magical Land.");
var nounBodyPart = prompt("Please enter the name of a body part."); 
var adjectiveBodyPart = prompt("Please provide a word that describes the body part.")
var verbOne = prompt("Please provide an action word (i.e. jumped, ran).");
var verbTwo = prompt("Please provide another action word (i.e. jumped, ran).");

var storyPerson = nounPerson.toUpperCase() + " " + "was famous computer geek from the magical land of";
var storyPlace = " " + nounPlace.toUpperCase() + ".  " + nounPerson.toUpperCase() + " " + "had a very";
var storyAdjBodyPart = " " + adjectiveBodyPart.toUpperCase(); 
var storyBodyPart = " " + nounBodyPart.toUpperCase() + ", which always helped when they"
var storyVerbOne = " " + verbOne.toUpperCase() + ".  ";
var storyAdjPerson = nounPerson.toUpperCase() + " " + "was considered a very" + " " + adjectivePerson.toUpperCase() + " " + "person"; 
var storyVerbTwo = " " + "who always loved to" + " " + verbTwo.toUpperCase() + "."; 

alert("Thank you for that.  Are your ready to read your story?");

var story = storyPerson + storyPlace + storyAdjBodyPart + storyBodyPart, storyVerbOne + storyAdjPerson + storyVerbTwo;

document.write(story);
P J
P J
2,391 Points

missing a + sign b/w storyBodyPart, storyVerbOne.

P J,

It's not a + sign that's missing; it would be a semi-colon. But, JS is very lenient with ending statements with a semi-colon or not, although it is good practice to always end statements with a semi-colon.

1 Answer

Hey Dagoberto Henriquez,

The only things you could do to simplify your code further would be to not initialize that second set of variables and have the variable story equal all of that concatenation that is going on. I also deleted quite a few of your " " strings because they are unnecessary when you are adding another literal string. For example,

//Unnecessary form
var helloWorld = "Hello" + " " + "World";

//Good form
var helloWorld = "Hello World";
var nounPerson = prompt("Please enter the name of a person");
var adjectivePerson = prompt("Please provide a word that describes the person.")
var nounPlace = prompt("Please enter the name of a City, Country or Magical Land.");
var nounBodyPart = prompt("Please enter the name of a body part."); 
var adjectiveBodyPart = prompt("Please provide a word that describes the body part.")
var verbOne = prompt("Please provide an action word (i.e. jumped, ran).");
var verbTwo = prompt("Please provide another action word (i.e. jumped, ran).");

var story = nounPerson.toUpperCase() + " was famous computer geek from the magical land of " +
nounPlace.toUpperCase() + ".  " + nounPerson.toUpperCase() + " had a very " +
adjectiveBodyPart.toUpperCase() + 
" " + nounBodyPart.toUpperCase() + ", which always helped when they " +
verbOne.toUpperCase() + ".  " +
nounPerson.toUpperCase() + " was considered a very " + adjectivePerson.toUpperCase() + " person who always loved to " + verbTwo.toUpperCase() + "."; 

alert("Thank you for that.  Are your ready to read your story?");

document.write(story);

You save a tiny bit of memory by not having to initialize all of those variables and by not having to initialize story independent of the content. It's negligible, though, so your way or that way will be totally fine. Happy coding! :)

Thank you very much!

Anytime, Dagoberto! If you have any more questions, feel free to ask away. =]