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 trialsebbe12
4,015 Pointswhile (randomNumber !== guess) { guess = getRandomNumber(upper); attempts += 1; }
while (randomNumber !== guess) { guess = getRandomNumber(upper); attempts += 1; }
I don't understand how this one works
2 Answers
Brendon Butler
4,254 Pointswhile (randomNumber !== guess) { // loop while the random number is NOT equal to the guess
guess = getRandomNumber(upper); // get a new random number and set the guess variable to this new number
attempts += 1; // increment the amount of guesses
}
// any code after this block will be run once the random number IS equal to the guess.
Basically, you're just looping until the computer "guesses" the correct number. And every time it makes a new guess, it adds 1 to the attempts variable. So that you could say something like "In 14 attempts, the correct number was guessed!"
Joshua Swilling
1,135 Pointshey sebbe i had a little rough time with this concept for a while , i developed a game out of it maybe this will help if you still had questions bro -- https://w.trhou.se/ft18pgwb4m
Dinesh Kumaar Rajendran
3,395 PointsHI Joshua Swilling,
Excellent explanation ! Thanks much !
sebbe12
4,015 Pointssebbe12
4,015 PointsI see we store the new getRandomNumber(upper) we get after each time the computer is wrong and store it to guess. I was thinking that guess was equal to getRandomNumber(upper) while it was not equal. I was like what.