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

Jason Force
Jason Force
1,759 Points

My Solution

Just thought I'd throw this out there for anyone needing to take a look at a solution. I'm fairly new to JS and this didn't crash in the brower, so I'm fairly certain it'll work just fine.

var num1 = parseInt(prompt("Enter a starting number."));
var num2 = parseInt(prompt("Enter an ending number."));
var randNum = Math.ceil(Math.random() * num2);
if (randNum < num1){
  randNum += num1;
}
alert("Your number is " + randNum + ".");
Jason Force
Jason Force
1,759 Points

I could have chosen better variable names, but hindsight is 20/20 right?

4 Answers

Jason Force
Jason Force
1,759 Points

Oh, no, nothing like that. I just saw that some folks had put up their solutions, so I thought I'd add mine on as well. Hope you didn't waste much time. Sorry. Thanks for looking though. :-)

Jeff Janes
Jeff Janes
8,033 Points

Not at all! Just looking to help :)

Jeff Janes
Jeff Janes
8,033 Points

I just ran this and it seems to work just fine! I enter a starting number and an ending number, and gives me a random number in between. :)

Does it not run/error out for you Jason?

It uses an if, which is not introduced yet in this course.

This is what I got:

var inputNr = parseInt(prompt('Give me a number'));

var inputHigherNr = parseInt(prompt('Give me a number which is higher than the one you gave me before'));

var randNr = Math.floor( Math.random() * (inputHigherNr - inputNr) ) + 1;

randNr += + inputNr;

document.write(randNr + " is some random nr between " + inputNr + " and " + inputHigherNr);

Thanks Jason, with your help I was able to write https://w.trhou.se/aol3fg5lef

I was having a hard time figuring our how to tie the prompt, parseInt and Math.random together and after going through this tread I got an idea and it worked so thank you for inspiring my solution.

alert("Hey Let's Play The Random Number Game! Click OK to start"); var numberCollected = parseInt(prompt("Write any number between 1 and 100")); var randomNumber = Math.floor(Math.random() * numberCollected) + 1; alert("Based on the number you gave, your random number is " + randomNumber);