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 Random Challenge Solution

Why they put "+" signal before and after the var amd, inside ""?

var message = "<p>" + randomNumber + " is a number between 1 and " + topNumber + ". </p>"

1 Answer

Tim Strand
Tim Strand
22,458 Points

They are concatenating together a p element in a string. then appending it to the dom. boils down to string + var as string + string + var as string + string resulting in a dom element like

<p>randomNumber  is a number between 1 and topNumber</p>

Hey Andre !

The main thing to understand is that in you cannot add a variable to a string when ITS WITHIN THAT STRING with just a + sign. Let me explain. When you have var message = "<p> + randomNumber + is a number between 1 and + topNumber + .</p>" (note: this is the (var message) variable without the surrounding string tags around the randomNumber and topNumber variables).

If you ran this line of code it will come out EXACTLY AS WRITTEN and will not be able to read the VARIABLES as actual variables, but will indeed read them as ONE WHOLE STRING !

So when you add string tags around the variables like topNumber and randomNumber(this turns them into independent strings) and add two + signs within that string (one on the left and one on the right), when you run the code it will recognize the contents within those strings as variables and not words and numbers within the main string(which is the
"<p></p>".