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
Ray Fan
558 PointsWow, I'm not smart enough to do this. The information is basic but the application is so hard
so he used three prompts for noun, verbs and adjective to store those into the variable and concatonated them with +=. That is so complex man! I don't think I'm smart enough for this.
2 Answers
Damien Watson
27,419 PointsHey Ray,
Not sure what the video is as the link doesn't connect, but it's all about breaking it down.
The prompt line takes the users response and stores in a variable. I'm assuming he does this three times and then concatenates them all together. The += is a shorthand way of saying var1 = var1 + var2; ==> var1 += var2;
I'll see if I can break it down for you:
var answer1 = prompt("What is your name?");
var answer2 = prompt("What is your favourite colour?");
var answer3 = prompt("What is your favourite shape?");
// All in one line
var concatString = "Hi " + answer1 + "! Your favourite colour is " + answer2 +
" and shape is " + answer3 + ".";
// or you can do the above line by breaking it down like:
//
var concatString = "Hi " + answer1;
concatString = concatString + "! Your favourite colour is " + answer2;
concatString = concatString + " and shape is " + answer3 + ".";
// then shorten it using the += :
//
var concatString = "Hi " + answer1;
concatString += "! Your favourite colour is " + answer2;
concatString += " and shape is " + answer3 + ".";
alert (concatString);
I don't know if this makes it easier or more complicated. I hope it helps some.
Ray Fan
558 Pointsthanks for the help. do I have to know all three ways?
Damien Watson
27,419 PointsIn time I would say yes though get your head around the basics and then start to use the += later to make your code smaller.
The first and second are pretty much the same. You define a variable to equal something and then later you may add to that variable.
There are a few of the 'shortcut' operators. The += is used with text, to join strings together (concatenate), but can also be used with numbers. The main operators are for add (+=), minus (-=), multiply (*=) and divide (/=).
var abc = 10;
var def = 5;
// Long Version:
abc = abc + def;
// abc = 10 + 5;
// same result using Short Version:
abc += def;
// This means abc = itself + def
But for the moment, if it makes more sense, just type out the long version.