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.
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 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
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!