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

It works but is it correct?

// Use Math.random() and the user's number to generate a random number const randomNumber = Math.round( Math.random() * highInput ) + lowInput;

I changed math.floor to math.round and also +1 to +lowinput (meaning the lowest is not 1 but the lowest number). I tested it and the answers seem to be correct. In my mind this was more logical and the code shorter. Any ideas?

1 Answer

Mark Casavantes
Mark Casavantes
2,880 Points

Good Morning Berit,

I believe you are correct.

let highInput = 100;
let lowInput = 0;
let num = Math.random();

for (let i = 0; i < 10; i++) {
  let randomNumber = Math.round(num * highInput + lowInput);

  let newRandomNumber = Math.floor(num * highInput) + lowInput + 1;

  console.log(newRandomNumber - randomNumber);
}