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

Can someone help translate this code for me?

I watch the videos and type the code and I understand some of it but when it gets into the complex lines I can't understand what the code means or how its doing what it is doing. Can someone Simplify or Tell me what it all means line by line?

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

Russell Sawyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Russell Sawyer
Front End Web Development Techdegree Student 15,705 Points

Hi Nick,

Hope this helps. :)

var input1 = prompt ("Please type a starting number");  /* User prompt for user to see.  creates a variable imput1 and assigns it a number that's given by the user */

var bottomNumber = parseInt(input1); /* Creates a variable bottomNumber and verifies input1 is an integer and stores that number into bottomNumber */

var input = prompt("Please Type a number"); /* Creates a variable input which is assign a number given by the user and stores that number in input */

var topNumber = parseInt(input); /* Creates a variable topNumber and verifies input is an integer and stores that number into topNumber */

var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber; /* math.floor(Math.random() rounds down the random number and then calculates a number between the topNumber and Bottom number that was input by the user */

var message = "<p>" + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + ".</p>"; /* Stores the string in the variable message */

document.write(message);  /* sends the result to the output screen */
jason chan
jason chan
31,009 Points

var message, the + are concat. Means it add the strings together. The abcd so it's all glued.

Hi,

The code on the first and third line is asking user to enter a number into the pop up window which line 2 and 4 change into an integer. You have to use parseInt function because all input from prompt is a string by default and the function will transform it into a number which can be used later.

'randomNumber' is an equation which generate a number between the two number that user entered. Math.random() generates a random number between 0 and 1 (exclusive) and Math.floor rounds the number down to its nearest integer.

I hope this helped. If not,maybe I would recommend to watch back the videos just to revised some of the concepts :)

jason chan
jason chan
31,009 Points

Your being asked by prompt ask a number. You answer a number it gets stored in the variable. Then the variable prints to the browser from random number simple algo.

parse means read by computer. Int well integer. Convert this guy into a number.

Thanks guys!