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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Enemuo Felix
Enemuo Felix
1,895 Points

How does the computer get the guess

This is a quote from Dave "If the number the computer guesses matches the number generated at the beginning of the program". So my question is , how does the computer generates its own guess which is compared to randomNumber in the while loop argument? Does JavaScript make the randomNumber code run twice at once to store one number at the randomNumber and, the other to serve as guess in order to compare both?

2 Answers

Tsenko Aleksiev
Tsenko Aleksiev
3,819 Points

Now I saw the video because I write from my phone: in there Dave sets a function getRandomNumber(upper) which picks a random number, he then has the var randomNumber ( your number on mind ), and in the while loop he uses the same function getRandomNumber on var guess ( for me to try to guess your number ... and this makes the game even longer ). The attemps var is for me to know how many times I have tried to guess your number. Sorry for my previous explanation I got you confused because I havn't watched the video. So in the while loop you use the same function again and again :)

Enemuo Felix
Enemuo Felix
1,895 Points

Thank you very much. I got it now!

Tsenko Aleksiev
Tsenko Aleksiev
3,819 Points

No problem, wanna play without the computer? You go first guessing :p

Tsenko Aleksiev
Tsenko Aleksiev
3,819 Points

The function at the beggining picks a randomNumber from 0 to the number chosen, let's say 10000. In the while loop javascript starts counting and comparing: first iteration, the counter is 1, is the random number 1, no? Then it loops again, the counter is 2, is 2 the random number, no - again and again and again till the loop gets to the randomly generated number from the function, the while loop can iterate 1,100 or even 10000 times till it matches the number. It's like this: imagine you and me sitting and you ask me to guess a number on your mind from 0 to 10000 and I ( because I don't have idea of your number ) start to ask you for each number from one to the upper limit you have told me...long game huh :) Computers are really dumm but they are really fast, that's why we are using them. Hope you got the idea :)

Enemuo Felix
Enemuo Felix
1,895 Points

Thank you very much Tsenko for the response, It's very thoughtful of you to use this example. This way I can use your example to explain my question better. So like you said I'm 'asking' you to get the random number by Initiating the random number function

function getRandomNumber(upper) {
  return math.floor(Math.random ()* (upper)+1);
}

So how do you (as the computer) get your own numbers to compare with mine? In other words, Which function generates your own numbers?