JavaScript JavaScript Basics Working With Numbers The Mad Libs Challenge Revisited

Javier Hernandez
Javier Hernandez
1,651 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,424 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
177,727 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.

Ewerton Luna
Ewerton Luna
Full Stack JavaScript Techdegree Student 14,346 Points

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."