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

Marcio Mello
Marcio Mello
7,861 Points

My Solution

Could anyone with a better knowledge than mine review my code (specialy the math inside the while condition)? As it is, the testing have been ok, but I am not sure if out of luck or if the math is right. Thanks.

var initialNumber = prompt("Gimme the initial value");

var lastNumber = prompt("Gimme the last number");

var randomNumber = Math.random();

while (randomNumber < (parseFloat(initialNumber)/parseFloat(lastNumber) - 0.1)) {

randomNumber = Math.random();

}

var dieRoll = Math.floor(randomNumber * parseInt(lastNumber)) + 1;

alert("You rolled " + dieRoll);

3 Answers

John Domingo
John Domingo
9,058 Points

I think the challenge is to find a random number between the initial and last values you accept from the user. You would not need to use a while loop for this problem because there is no need to iterate any of the steps. All the heavy lifting is done for you by Math.random(). You just need to take the random number generated by the Math object and constrain it so that it is always between the initial and lats values you prompted for.

Good luck and just reply back if I don't make any sense.

John Domingo
John Domingo
9,058 Points
  • Your solution will not roll the lower number becuase the inequality is basically saying if randomNumber is less than a certain ratio, keep rolling until you are greater than that ratio. That is like saying if you roll anything less than 3, keep rolling until you get a 4, 5, or 6. You never end up with a 1, 2, or 3.

To revisit my last reponse, I say that the while loop is "uncessary" because it is inefficient. For a large dataset, it would run too slow especially for a problem that does not require any iteration. It should be noted that your code seems to be for a die roll (only 1 to 6) but I think the question is asking for any range of numbers from 1 to any number, so keep in mind that efficiency is key.

In general, you want to keep your code as uncomplicated as possible because while the point is to code up a program to automate complicated processes, we still have to keep in mind that computers are essentially not that smart, and it really comes back down to the programmer who has to read the code and debug it. The code needs to be intuitive and logical and straight to the point. If you plan to apply for a tech job and have a coding interview, I don't think having the the correct solution is enough; the methodology is just as, if not more, important.

Marcio Mello
Marcio Mello
7,861 Points

You do make sense John. I was just wondering if, although more "complicated" and with unecessary code, my solution was ok. The problem I have with it is that it seems that the lower number never gets rolled. Thanks.