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

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
5,913 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
231,269 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."