Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

rocky roy
rocky roy
556 Points

RandomNumber and guess have equal value, because inside the loop we are assigning RandomNumber to guess.

RandomNumber and guess have equal value, because inside the loop we are assigning RandomNumber to guess. That mean at first time computer should guess the number .

while (guess !== RandomNumber) { guess = GetRandomNumber(upper); // both have the same number atttems += 1; }

Then why computer taking so long time to guess the correct number.

3 Answers

Steven Parker
Steven Parker
216,136 Points

It's not clear that "inside the loop we are assigning RandomNumber to guess". For that to be true, RandomNumber would have to be assigned inside GetRandomNumber to the same thing as the return value. But you did not show that part of the code.

Based on your observation of the "computer taking so long time to guess the correct number", I would expect that is NOT what happens inside the GetRandomNumber function.

For a more complete analysis, you need to share all the code, or better yet, make a snapshot of your workspace and provide the link to it.

Steven Parker
Steven Parker
216,136 Points

Just what I thought, the GetRandomNumber function does not internally change RandomNumber, it just returns a random guess.

So on line 2, RandomNumber is established:

var RandomNumber = GetRandomNumber(upper);

Then, inside the loop, a new random number is picked and assigned to guess. The loop continues until the original value is picked again, causing guess to be the same as RandomNumber.

Given the range used (10000), you can expect that the average number of guesses will be about 5000 (half the range). But when the generator is "unlucky" it could take many times the range to get it.

Just for grins, I tried running this 10,000 times. The smallest number of guesses was 2, and the greatest was 98040 (because the "guesser" doesn't remember what was already tried). The average number was 2716, which means the computer was actually pretty "lucky" overall (or more likely, the random number is not truly "random").

rocky roy
rocky roy
556 Points

var upper = 10000; var RandomNumber = GetRandomNumber(upper); var guess; var atttems = 0; function GetRandomNumber(upper) { return Math.round(Math.random() *upper) + 1 } while (guess !== RandomNumber) { guess = GetRandomNumber(upper); atttems += 1; } document.write('The random no was '+ RandomNumber + ' <br>'); document.write('computer took ' + atttems +' times to guess it');

my main concern is how guess is getting number.

Steven Parker
Steven Parker
216,136 Points

See the comment added to my previous answer above. :arrow_heading_up:

rocky roy
rocky roy
556 Points

now i understood it properly. thank you very much for explaining it.