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!

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

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.