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

Jamilur Rahman
Jamilur Rahman
3,336 Points

My Code works really well. Can someone check out my code to see why it is so different the video solution?

var input = prompt('What is the first number'); var input1 = prompt('What is the end number');

var firstNum = parseInt(input); var endNum = parseInt(input1);

var roll = Math.floor((Math.random()*endNum) + firstNum ); var message = "<p>" + roll + " is a number between " + firstNum+ " and " + endNum + ".</p>"; document.write(message);

alert(roll);

Hi there, what video are you referring to ? be sure to create questions from video lessons page.

Jamilur Rahman
Jamilur Rahman
3,336 Points

It is not really a question. It is a coding challenge for java script. they want me to ask for first num and second num. then make it into a range. i just wanted to know if it elogic for my var roll is correct

1 Answer

Steven Parker
Steven Parker
230,274 Points

The formula shown here is likely to generate a number well outside the requested range. For example, try asking it for a number between 1000 and 1010.

The classic formula (adapted to these variable names) would be:
ā€ƒ Math.floor((Math.random() * (endNum - firstNum + 1)) + firstNum;