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 Numbers The Math Object Random Number Challenge – Two Numbers Solution

karina037
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karina037
Full Stack JavaScript Techdegree Graduate 17,179 Points

My solution - review/comments appreciated :)

hi all,

my code is obviously different from the teacher's solution, but grateful to hear if you think it will work for all cases.

link to workspace https://teamtreehouse.com/workspaces/41938569

const userInputMin = prompt ("Insert any number");
const userInputMax = prompt ("Insert any number that is greater than the first inserted number");

const numberMin = parseInt (userInputMin);
const numberMax = parseInt (userInputMax);

if (numberMin >=0 && numberMax) {
  const randomNumber = Math.floor ( Math.random() * numberMax) + 1;

  if (randomNumber < numberMin) {
 console.log (numberMin);
} else {
 console.log (randomNumber);
}
} else console.log ("try again");

Can you put it in a Workspace and share the link?

karina037
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karina037
Full Stack JavaScript Techdegree Graduate 17,179 Points

hi Ryan, thanks for your comment - edited my post and included the link just before the code. Hope it will work :) looking forward to hearing your thoughts

karina037
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karina037
Full Stack JavaScript Techdegree Graduate 17,179 Points

Hi Benjamin, thanks for checking the code. You are right. I guess one way to improve the code is to change the condition when a random number is logged out only when randomNumber >= numberMin. All the other cases are omitted then. What do you think?

Well, you won't be throwing the minimum number as much, but the higher the minimum number, the more "try again" will be logged. It's a bit ahead of this lesson but perhaps the code could be rolled up into a function, then if conditions aren't met, use an else clause to repeat the function.

1 Answer

That's a creative way of doing it, however the odds of random change. Correct me if I'm wrong, but You will be more likely to get a minimum number because every value under it will automatically become that. Also then the larger your minimum number is, the more likely you will get that minimum number. For instance if you did 1000 to 1100, you will get 1000 many times before you get a vaule between those two numbers.