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.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Alexander Gyger
Alexander Gyger
14,237 Points

How to prevent the program from guessing the same wrong number multiple times?

Maybe it's a bit out of scope for this exercise, but when I ran this random guessing program and printed out the attempts to the console, i got this: 1434 attempt - guess: 764 app.js:21:3 1449 attempt - guess: 764 app.js:21:3

So it guessed the wrong number 764 at least twice, and other wrong numbers probably as well.

If this was a competitive program to guess numbers, this would be quite ineffective. To make it better it would have to be able to consider all prior wrong guesses.

How would one approach that? With storing and recalling? Or would that make the program slower?

1 Answer

pooya tolideh
pooya tolideh
12,184 Points

One quick way to tackle this is to write an algorithm like the colder-hotter game.

If the guess is more than the number you have in mind, you create a new range with the guess as upper limit If the guess is less than the target, then you need to recreate the range and set the guess as lower limit.