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

Where does the <p> come from/how does it work?

Hey everyone,

In the code to this video - how does this < p > aspect work in this video? I don't remember Dave going over this.

Thanks in advance

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

Hey Ramsey, what do you mean by aspect work?

the <p> is a HTML element short for paragraph. In the example you gave if the variable message is printed to the screen the contents will be wrapped in the HTML <p> tag.

5 Answers

the objects in the variable message are concatenated linearly through the + operator. When we use the < p > at the start and the < /p > at the end of our variable we are wrapping the concatenated objects inside the < p > tag, so when we print to the viewport, the variable message is printed as inside a < p > tag.

It relates to the video as Dave is using the <p > tag to format the content inside the variable message and make it semantically correct.

here is an example:

<p> 3 is a random number between 1 and  5 </p>

this is just pure HTML. I used the number 3 for the variable randomnumber and 5 for the topnumber variable.

now for the JS version

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

document.write(message);

if the random number and the topNumber stay both 3 and 5 just like for the HTML they will print the same.

I hope this helps, if you want to learn more about this please take a look at:

http://www.w3schools.com/js/js_operators.asp

http://www.w3schools.com/js/js_htmldom_nodes.asp

Much more technically correct than mine haha' Congrats Jacob, perfect job.

Thanks a lot Jacob - and to everyone -

This was very helpful - I get it now

Ramsey, not a problem. I'm glad it helped.

aspect? Sorry ramseydennis, I didn't understand your question.

Hey William - and also Jacob -

I guess because it was a form of code, the editor wasn't allowing me to display < p > in front of "aspect" in my question -

I have edited my question so you can see what I mean -

Thanks again

Ramsey,

the < p > is a HTML element short for paragraph. In the example you gave if the variable message is printed to the screen the contents will be wrapped in the < p > HTML tag.

Thanks Jacob -

but can you be more specific or provide an example as to how the contents will be wrapped in the HTML tag, as it relates to JavaScript and this video?

Thank you again -

Hey ramsey,

I think I got it now. As Jacob said, the

"< p >"

is an HTML tag. And the

" var message "

is the javascript variable. Give it a try, copy and paste this code inside a Script tag in a blank HTML file:

    var message =  "<p>" + 6 + " adding number six to html." + 5 + " and number five</p>";
    document.write(message);

You'll see that you can write, or control content inside HTML files using JS.

In case you don't know how to do the HTML file, here it is:

<!DOCTYPE html>
<html>
<head>

</head>

<body>

<p>test</p>

<script type="text/javascript">
<!--INSERT CODE ABOVE HERE-->
</script>

</body>
</html>
jason chan
jason chan
31,009 Points

it's from html.

Your supposed to mangle all your code. When your doing web all the code gets mangled into one to create an app. I hope it helps.

Basically, every language you learn for code. They all can work together.