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

3 Answers

Erik Nuber
Erik Nuber
20,629 Points

I see. It might be a better solution to check the two chosen numbers against each other. Whichever is bigger you than use as the upper in your random number generator.

var upper, lower, aRandomNumber;

function randomNum(upper, lower) {
    return Math.floor((Math.random() * (upper - lower)) + 1);
}



var num1 = prompt("Please Pick Any Number");
var num2= prompt("Please Pick A Second Number");




while (num1=num2) {
    num2= prompt("Please Pick A Number That is Not The Same As The First");
}

if (num1 > num2) {
   upper = num1;
   lower = num2;
} else {
   upper = num2;
   lower = num1;
}

aRandomNumber = randomNum(upper, lower);   //calls the function, you can do with it as you like
Stefan Robert Stegaru
Stefan Robert Stegaru
8,554 Points

Thank you , it is a bit over what I have learned it till now , I just hope I will be able to replicate it after I learn a bit more.It was a challenge from the videos .

Erik Nuber
Erik Nuber
20,629 Points

You will get there and be fine. They reuse the random number generator quite a bit. When you mentioned doing the larger number versus smaller number I thought you were farther along in the courses. You are doing well just keep at it. It takes patience and sometimes watching and rewatching the same videos to understand.

Erik Nuber
Erik Nuber
20,629 Points

Your code ends up with an endless loop that can only be exited by checking a box to stop displaying the alert boxes.

Stefan Robert Stegaru
Stefan Robert Stegaru
8,554 Points

Yes I have seen it when I tried to put a smaller second number.Now it should work if you try it again.

Erik Nuber
Erik Nuber
20,629 Points

I am not sure what the program is supposed to be doing. It has some issues. It asks multiple times for the second number. I entered 5 first, than 1 and it asks me to enter a number then bigger than the first. So I entered 8 and, it keeps asking me.

When I click cancel it prints something to screen.

Stefan Robert Stegaru
Stefan Robert Stegaru
8,554 Points

It should let you to insert two values and give a random value between them.But in order to be sure that the second number is bigger than the first I have tried to insert a loop.