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


Stephen Brown
Stephen Brown
Full Stack JavaScript Techdegree Student 3,511 Points

Thoughts on whether this can be refactored to be more simple/streamlined...


I wanted to get some feedback to see if my code for this can be refactored into something more streamlined?

var startNumber = prompt('Please provide the first number.');
var endNumber = prompt("Thank you! Now please provide the end number.")

var yourNumber = Math.floor(Math.random() * (parseInt(endNumber) - parseInt(startNumber) + 1) + parseInt(startNumber));
document.write("<h2>Your number is " + yourNumber + ".</h2>");

I feel like it is somewhat close, but if it can be better, it would be helpful to know so that I can improve going forward!


1 Answer

David Bath
David Bath
25,940 Points

I don't think it can be streamlined much, but one thing you could do that might make it a little easier to read is to parseInt while retrieving those values:

var startNumber = parseInt(prompt('Please provide the first number.'));
var endNumber = parseInt(prompt("Thank you! Now please provide the end number."));

var yourNumber = Math.floor(Math.random() * (endNumber - startNumber + 1) + startNumber);
document.write("<h2>Your number is " + yourNumber + ".</h2>");