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 Review Working with Numbers

I have a question on the Random Challenge. I am getting lost with the prompt commands, parseInt, Mathfloor, etc.

I am struggling making sense of a few things in the JavaScript coding. Can someone help me out?

Below is a code that the instructor asked me to put together asking a user to input two random numbers and come up with coding on how to structure this. I am confused as to the following items in this code:

1) where to use ' versus " 2) this statement:
'var message = "<p>" + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + ".</p>";

Why does the "<p>" have quotes around it? Why do I put a + sign before and after randomNumber/ bottomNumber?? Finally, why is the quote close before the period and then after the closing paragraph tag? ".</p>"

Thanks. Bryan

var input1 = prompt("Please type a starting number"); var bottomNumber = parseInt(input1); var input = prompt("Please type a number"); var topNumber = parseInt(input); var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber; var message = "<p>" + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + ".</p>"; document.write(message);

4 Answers

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

var message = "" + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + ".";

Let's break up this string piece by piece.

You started out with quotations implying you started a string and end it immediately, Now why did you do that? Because the next value in your string is something you want from the coding you have done and stored it in the variable called random number. Here random number in the final output will display what's stored in your variable. Change the random number's value to 2, you will have 2 in your final string.

And we add this symbol "+" to concatenate basically or just add two different stuff. Now if you had put random number in the quotation marks as well, It would not have displayed a number=2, it would have literally replicated the random number string.And if you add a variable inside a string for the final output you just close the quotations and concatenate it with your variable. Now it's confusing right? Let me give you an example.

Lets just say my variable have these values:

randomNumber =2;

bottomNumber= 1;

topNumber =3;

var message = "" + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + ".";

output will be--->"2 is a number between 1 and 3" Now just notice how variable have been changed.

Take another eg. Here I have put everything in a single string.

var message = " + randomNumber + is a number between + bottomNumber + and + topNumber + .";

Guess the output??

It will be this:

  • randomNumber + is a number between + bottomNumber + and + topNumber + .;

SO, in a nutshell,Your fixed part in the string, which you don't want to change goes inside the quotations and the changing part,i.e, stored in some variable you concatenate it with a string.

I hope you understand. If you feel You still have any questions, feel free to ask it.

brandonlind2
brandonlind2
7,823 Points

If I'm understanding correctly you dont understand the difference between '' and ""? there isnt a difference they can be used interchangeably, the only time you would use them together is if you had a quote inside a string, for example var saying = " tom said 'hi how are you' "; the "" dont have quotes around them they are separate stings being added together. they are separeate pairs of "" that come after each other. Sentences and words go in "" words without "" around them are varible names the first set of "" is probably supposed to have a space in it so there is a space before randomNumber and you're puttng a + after bottomNumber because you want a space and the word and to be added after the contents of the bottomNumber var

var message = " " + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + ".";

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

Best way to understand these things is open your console and You'll see the differences. Console and the documentation are your best friends now.!

Thanks guys. All these answers are great. I'm still struggling with the strings but am looking at different sites and reviewing these videos to try and educate myself. You all have a great handle on writing this code.

Bryan