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

Aren't we cheating against the computer a bit?

If I am understanding this code correctly, the first attempt made by the computer is before a guess is established, since the first pass through the loop is necessary in order to give the guess variable any value.

Therefore, I would think it would be impossible for the computer to guess correctly on its first attempt.

Doesn't that mean that in order to get a fair result, we would have to take the final value of the attempts variable and subtract it by 1?

3 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Jesse Vorvick! You somewhat answered your own question here. That's exactly the reason we start attempts at 0. When it goes through the loop the first time, it will get the guess and the random number then increment the attempts, regardless if it was correct or not. But if it was correct the first loop, that means that attempts will be equal to 1 which is correct. The computer got it correct on the first try :smiley:

Hope this helps! :sparkles:

What I'm saying though, is that the way the code is set up in the video, it appears to me to be impossible for it to be correct on the first loop.

while ( guess !== randomNumber) {
  guess = getRandomNumber( upper );
  attempts += 1;

The guess variable is not given a value until the first pass through the loop has already begun, because ( guess !== randomNumber) is false no matter what (var guess having no value). Then when the loop ends, the computer has 1 attempt against it, even though it had nothing to guess with. So, it seems strange to me that the first pass through the loop is counted as an attempt, since from my limited point of view there is 0% chance for the computer to guess correctly on the first attempt, having no guess to work with in the first place.

So what I'm saying is, wouldn't it make more sense to have var attempts set to -1 instead of 0 at the beginning of the code, or to add var attempts += 1 at the end after the loop?

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Jesse Vorvick I think a lot of this has to do with how we speak about guesses. But you said this:

( guess !== randomNumber) is false no matter what (var guess having no value)

Except that it's the opposite of that. The first time that while is evaluated it will always be true. Guess will never be equal to the random number at the first evaluation because it hasn't yet guessed. That's what sets the wheel in motion, so to speak. Then when the loop starts to execute, it finally gets to guess. At that point, after the first guess, we raise the attempts to 1. Then we re-evaluate. Was the guess still not equal to the random number? If it guessed correctly on the first try, that loop won't run again and attempts will remain at 1.

If you and I were playing a game where I asked you to pick the number I'm thinking of, is there any scenario where I could say that it took you 0 attempts? No. You have to make at least one attempt. A little like golf. The best you can get is a "hole in one". You can't get a "hole in zero". You have to hit the ball at least one time :smiley:

Does this clarify things? :sparkles:

Thanks for your reply!

I misspoke. I meant it's true no matter what, yes. But I still don't understand how this is not unfair to the computer. It seems that the best the computer can get is a "hole in two," not one.

When it first runs the loop I re-posted in my previous comment, no matter what the condition for "while" is true. Meaning, the computer's guess (which is empty) does not match the randomNumber variable. As you said, this sets the loop in motion. The first thing the loop does is put a value in the guess variable. The second and only other thing it does is change the value of var attempt from 0 to 1. So now, in its first pass, the computer already has a strike against it (like golf). It had no chance.

The way I see it, in the first evaluation you were talking about in your previous comment, the computer did guess. Not only did it guess, but it had no choice but to guess "nothing." And even if it hadn't "guessed," the bottom line of what I'm saying and why I'm confused is that it received a "stroke" against it on it's first pass through the loop. Since there is a possibility that it could guess correctly on the second pass through and not the first, the highest (lowest) score it could get (attempts) would be two.

I could be interpreting this the wrong way, but I don't feel like my question was made clear.


I get it now! Even though it seems unfair at first because it can't get the first pass correct, I had missed the fact that when it DOES get it correct, because while ( guess !== randomNumber) would be false in that case, the loop doesn't run and therefore its correct "attempt" isn't counted! It's like it retroactively fixes the "unfair" strike against it in the beginning by not counting the correct one, thus already indirectly doing that whole -1 attempts thing that I thought should manually be put in.

Thank you for your patience while I wrap my head around this, I was looking at the start of the code when it runs and missing the exact course it takes at the end when the loop is "closed off" because of while() becoming false.

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi again, Jesse Vorvick ! Yes, it is fully possible for the computer to guess it on the first try. Let's take a look at the code line by line though this is a subset of the code for brevity. Let's also assume that the upper number is not 10000 but instead... 5. Let's say 5.

var randomNumber = getRandomNumber(5);
var guess;
var attempts = 0

while(guess !== randomNumber) {
   guess = getRandomNumber(5);
   attempts += 1;

By setting up the while loop to always evaluate to true on the first encounter, we are guaranteeing that the computer gets one guess.

Let's say for instance that randomNumber gets assigned a 3. That's what was returned from the getRandomNumber. When we get down to the while it asks "Was undefined not equal to 3"? Yes that's true... so proceed to the inside of the while.

Inside the while we then get a guess. Let's say the guess then uses the getRandomNumber and it also gets a 3. Well now our guess is equal to our random number. The attempts is now equal to 1.

So we go back up to the top of the while and ask "was 3 not equal to 3?". That is false so none of those will run again including the increment on the attempts. Attempts is still equal to 1.

It would print out that the result was found in one try (or the number of attempts).

Is that clearer? :sparkles:

Haha nice, thanks for your promptness. Funny enough, I had actually changed var upper to 5 and tested it that way after my first comment (as of today) and before you posted your reply which happened to use 5 as an example! And that was when it clicked for some reason. Crazy!