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?
Jennifer NordellTreehouse Staff
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
Hope this helps!
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.
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!