Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,019 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