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

Seth Ellis
PLUS
Seth Ellis
Courses Plus Student 5,950 Points

I don't comprehend the syntax ' [ ' + questions + '] and how that produced the number 3 from var questions = 3

I don't comprehend the syntax ' [ ' + questions + '...] and how that produce the number 3 from var questions = 3 to be utilized in the math.

I understand how the math worked in the javascript statements but the one stated above is throwing me off.

Oscar Mauricio Lancheros Murillo
Oscar Mauricio Lancheros Murillo
3,608 Points

Hi, you have an intial value asigned to questions of 3.

The '[' + questions + ']' simply concatenate the parenthesis with actual value of variable that in this case is equal to three.

Let me know if you get me.

4 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Seth,

In Javascript, the + symbol can be used as math, or for what's called "concatenation", which is just mashing strings (or text) together. In this example,

'[' + questions + '...]'

turns into "[3...]". Don't let the square brackets throw you off! They're just text in this case.

Here's how you might use it if you were coding a game that reported the users score:

var score = 10000;

alert('Good job! You scored ' + score + ' points!!!');

This would display an alert with the text "Good job! You scored 1000 points!!!"

Make sense?

Hi Seth!

By these lines in the beginning of your code

var questions = 3;

you are adding the value 3 to your here defined variable questions. The value of questions is now 3 for the following usage of the variable questions.

The syntax:

' [ ' + questions + '...] '

is basically forming a string that is supposed to be displayed as a term like this: [3 ...] because the current value of the variable questions is 3 according to the first variable declaration above :) Since the value 3 is assigned to the variable questions without quotation marks ('' or "") around it, the value 3 will be treated as a an integer data type. JavaScript treats the 3 as a true number so to say, that can be calculated with. If you put quotation marks around the 3, like so: var questions = "3";

JavaScript would interpret this as a string and you could not do any Maths with it. You'd have to parseInt the variable first.

Hope this clearifies it for you :)

Jim McQuarrie
Jim McQuarrie
10,597 Points

I had the same issue and had to play with it to get it, would have been a lot easier understanding without the square brackets throwing me off. So I wrote it like this:

var questions = 3;
var questionsLeft = '. There are ' + questions + ' questions left';
var adjective = prompt('Please type an adjective' + questionsLeft);
questions -= 1;

Hope this helps, Jim