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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Christopher Borchardt](https://secure.gravatar.com/avatar/401091cfc0d43b521634d06c3fe917b5?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Christopher Borchardt
2,908 PointsFault 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.
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](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsI'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
2,908 PointsChristopher Borchardt
2,908 Pointsthank you for the hints, i did in fact make it work.
https://w.trhou.se/a9s08fg57q
that is my current code.
Christopher Borchardt
2,908 PointsChristopher Borchardt
2,908 Pointshttps://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.