DEV Community

NovaJay2415
NovaJay2415

Posted on

Review my JavaScript? #2

Hello to all the experts of JavaScript!

I've worked on a fairly easy piece of code here (to me it's quite extensive since I'm just starting out) that I would love some pointers on.

Here's the link to it: https://codepen.io/Nova/pen/RmEYoZ

Now I know, I give the answer to the missing number in the JavaScript code. That's actually one of my questions. So let me begin:

First, I want to be able to randomly generate a number that users have to guess, since just one look at my JavaScript code you know automatically what the correct guess is. I need to use something like Math.floor and Math.random... but I don't know how to apply it to my variable to make it work.

Second, is there an easier way I could have done this?

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

Fourth, if there is something I could have done better please let me know.

Thank you guys for reviewing my code! I really appreciate it.

Top comments (5)

Collapse
 
coreyja profile image
Corey Alexander • Edited

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!

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.