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) Working With Numbers The Mad Libs Challenge Revisited

Alan Sea
Alan Sea
6,781 Points

About concatenating in this exercise...

Since var questionsLeft is a simple String assignment created by combing var questions with the message that follows, why does the + have to go inside the quotes surrounding var questions...? Place the + outside quotes and it fails at runtime. What gives? Aren't we treating var questions as a string either way??

7 Answers

jack AM
jack AM
2,618 Points

Ah I see what you mean, no worries.

If you put a + sign inside quotes, either double or single, it gets treated as any other letter of the alphabet!

var expression = "4 + 2"; 
console.log(expression); //outputs        4 + 2

Because what if you really wanted to use the + sign as part of a sentence in a paragraph on your website??? And NOT treat it as an actual addition operator, how would you do that? You would have to put the + sign inside some quotes.

var sentence = "The addition sign, +, is a math symbol!";
console.log(sentence); //outputs  The addition sign, +, is a math symbol!
//You actually see the + sign in the sentence

BUT, if you put the + sign outside of quotes, the javascript interpreter treats the + sign like it is actually meant to be treated in math (adding numbers together), or as a way to concatenate variables and strings together.

var sentence = "The addition sign " + " is a math symbol!";
console.log(sentence); //outputs  The addition sign is a math symbol!
//notice you don't actually see the + symbol?

Now as for this statement...

var questionsLeft = "[" + questions " + ]"

Since the + sign is inside the quotes on the far right, you're telling the javascript interpreter, that you actually want the + sign treated like you would any other letter of the alphabet in a string. So there is a problem because the questions variable is in between two strings, the "[" string, and the "+ ]" string, and there is only one + sign that is ACTUALLY concatenating the variable questions, the one on the left. So depending on what you want, you can do either two things...

var questions = 3;
var questionsLeft = "[" + questions + " + ]";
console.log(questionsLeft); //outputs [3+]

or...

var questions = 3;
var questionsLeft = "[" + questions + "]";
console.log(questionsLeft); //outputs [3]
jack AM
jack AM
2,618 Points

Because what is happening is you're building up a string, and anytime you want to add a variable to a string you must do so with the

  • sign...
var questionsLeft = "[" + questions; // will be [3

right? So if you want to continue adding something after the variable, then you must place another + sign behind it...

var questionsLeft = "[" + questions + "]"; // will be [3]

Although I'm not sure what you mean when you say place the + outside the quotes, can you give me an example of that? Because you're right the end result is that var questions will be a string.

Also I believe your var noun statement is two lines above where it is suppose to be, and missing the questionsLeft variable behind it.

jack AM
jack AM
2,618 Points

Would you mind pasting your code so we can see whats happening?

Alan Sea
Alan Sea
6,781 Points
var questions = 3;
var questionsLeft = "[" + questions + "  questions left. ]";
var adjective = prompt('Please type an adjective' + questionsLeft);
questions -= 1;
questionsLeft = "[" + questions + "  questions left. ]";
var verb = prompt('Please type a verb' + questionsLeft);
var noun = prompt('Please type a noun');
questions -= 1;
questionsLeft = "[" + questions + "  questions left. ]";
alert('All done. Ready for the message?');
var sentence = "<h2>There once was a " + adjective;
sentence += ' programmer who wanted to use JavaScript to ' + verb;
sentence += ' the ' + noun + '.</h2>';
document.write(sentence);
Alan Sea
Alan Sea
6,781 Points

We know this line of code works:

var questionsLeft = "[" + questions + "]"; // will be [3]

Why won't this?

var questionsLeft = "[" + questions " + ]"; // doesn't compile after + outside of the quotes

Also, thanks for mentioning the noun error. That was a simple copy/paste mistake.

Alan Sea
Alan Sea
6,781 Points

Makes complete sense. Thanks a lot!

jack AM
jack AM
2,618 Points

Yea you bet! If you don't mind, can you up vote my answer, so that anyone coming across a similar question can see my post as a solution :)

I personally think the confusion is coming from the fact that the [square brackets] are in their own sets of "quotes" for the purpose of PRINTING to the page. I initially had the same thought... I assumed the [square brackets] were part of the equation (NOOB I KNOW) as some sort of new character set we can/should use ( like we use pairs of (parenthesis), {curly brackets}, etc...)... It looks like:

var questionsLeft = "[" + questions + "  questions left. ]";

I initially I thought the OUTERMOST quotes were a pair (ie "[...string...]", and that the innermost quotes were a pair (ie " + questions + ")

But now I see that the [square brackets] are to be part of our text string... so what would "print" would be : [3 questions left.]