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 trial

JavaScript

Christopher Borchardt
Christopher Borchardt
2,908 Points

Fault catching in guessing game

I am working on my number guessing javascript game. As the code is written it works fine. The only problem I have is if the user puts in text, or a blank on the actual guess there is no catch for it like there is if the incorrect guess is put in when selecting the range for the guessing game.

https://w.trhou.se/a9s08fg57q

I have tried several different ways to get a similar while loop to run where the prompt is for a guess, but every time it breaks the code.

1 Answer

Steven Parker
Steven Parker
230,274 Points

I'm guessing you probably copied the starting and ending of your while and left off the return at the end, right?

The catch is while (failed = true) will never end, since just one equal sign (=) is an assignment, not a test. It works in getNumbers because of the return, which ends the loop and the function.

Also, when testing a value for true, you don't need to compare it at all, just name it, like this: while (failed).

Some other things to keep in mind while creating your loop:

  • be sure to declare any variables before the loop (not inside) if you will use them after the loop (like "guess")
  • be sure you use parseInt on your input before you test it with isNaN
  • be sure you are comparing numbers (currently else if (guess < randomNumber) compares a string to a number)

With these hints, I will bet you can make it work now.

Christopher Borchardt
Christopher Borchardt
2,908 Points

thank you for the hints, i did in fact make it work.

https://w.trhou.se/a9s08fg57q

that is my current code.

Christopher Borchardt
Christopher Borchardt
2,908 Points

https://w.trhou.se/cml5kwuzkd

ok this should be my last version. in this version I added a check to make sure the guess isn't lower than the low end, or higher than the upper end selected by the user.