DEV Community

Discussion on: Review my JavaScript? #2

Collapse
 
novajay2415 profile image
NovaJay2415

Please share what you have in mind! I would love to see how you would do it. Thank you for offering to do so!

I think the thing that bugs me is that once you get to the third guess it still tells you either "Higher" or "Lower" when it should just go to "you failed" after already trying 3 times. But, I don't know how to make it do that as of right now.

Thank you so much! I really appreciate your feedback. :)

Collapse
 
coreyja profile image
Corey Alexander

So here is a pen that is the closest to what you started with: codepen.io/anon/pen/PvXXqO

What I did was first I changed the condition for the while. Instead of just userGuess !== chosenNumber I added && userGuessCount < 3 so that the loop also stopped when the user guessed too many times. Then I was able to take the second if/else that was INSIDE the while OUTSIDE cause it only needed to happen when the game was over. This way I was also able to remove the breaks since the while loop conditions alone were enough to know when the loop was done.

But this ^ does not solve the problem you mentioned about saying higher or lower after your third guess. But I did also rewrite a bit of this and commented it up nicely for ya! Let me know if you have any other questions !

Thread Thread
 
novajay2415 profile image
NovaJay2415

Corey!!! I love what you did and wow you made it preform so much better!!! Thank you for all the comments in the code, I will be studying this to make sure I can apply this into my future projects.

I like the fact you don't have to reset the browser or use any breaks! That makes it so much cleaner and easier to understand.

Thank you very much!

Thread Thread
 
coreyja profile image
Corey Alexander

I'm glad you liked it and you found it easy to understand! You had a great start I just tweaked it a bit! You even knew what functions to call for the random number generation!

Ya breaks definitely have their place but I think it can be easier to reason about without them sometimes.