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

Javier Hernandez
Javier Hernandez
1,822 Points

Confusion with the number string

Why do I need to place + on both sides on the inside of the "questions" variable like so: ... ' + questions + ' ... I would think that it would be: ... ' questions ' + ...

3 Answers

Jay Dacosta
Jay Dacosta
3,425 Points

I struggled with the writing of it at first too. It's because the [] brackets make it feel like its part of the equation but really its just aesthetics as a string. The code would work the same if it was just:

var questionsLeft = questions + " questions left";

this would result in "3 questions left".

But he adds the [] brackets to add to the look of the string and stand it out from the original text. He wants this:

[ 3 questions left ]

because of that you have to add the brackets as strings before the variable questions and on the end of the string hence the plus sign on both sides. Hope that helps

Steven Parker
Steven Parker
216,014 Points

The + sign is the concatenation operator, it tells the system that you want to join the contents of that variable to the strings on either side. You need one for each join, so one on each side.

You need to do that because you are joining (concatenating) 3 individual pieces of the final string you want to display.

  1. The first part is the string: " ["
  2. Then, you want to concatenate the number: + questions
  3. Finally, you will join the last part: + "] questions left."

After concatenating these 3 parts, you will get: " [3] questions left."