re: Review my JavaScript? #2 VIEW POST

FULL DISCUSSION
 

Third, did I "break" the while loop the right way?

The code you have definitely works correctly and breaks out of the loop correctly!

But when you look at the code, do you think you it is possible to exit the while loop because we violated the condition it checks, or are we always going to hit a break statement?
(I have something in mind so let me know if you want me to write it up quick for ya!)

If we are always going to hit a break statement, is there a different way we can write the code so that we lean into the while loop condition?
I think that sometimes adding break statements to loops can make the logic harder to follow, since it's harder to immediately see all the different times you may exit the loop.

Overall I think this is really good! It definitely does what it's supposed to and is very readable code!

 

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. :)

 

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 !

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!

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.

code of conduct - report abuse