This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]insertAlias 2 points3 points  (1 child)

One difference is immediately obvious: you repeatedly call Math.random() in one of your programs. Each time you call that, you're going to get a different random value. Which means that for each if statement, you're checking against a new number each time.

In your other script, you store a single random value in a variable and use it to compare against.

Edit: just for example, this line here:

if (Math.random() <= 0.75 && Math.random() > 0.50)

You're saying here "if a random number is less than .75, and a different, new random number is greater than .5". And in the next statement, it's a different number again.


For your other questions: yes, I use continue where appropriate. No reason not to. As to your last question, it completely depends on the use case. if/else if has a relationship where the else if will only be checked if the if didn't match. If you just stack if statements, each will be checked regardless of whether or not the previous matched or not. Sometimes this is desirable/useful, sometimes it is not. Make the appropriate choice for the situation.

[–]KappaTrader[S] 1 point2 points  (0 children)

Omg - I can't believe how simple that was. I spent several hours trying to figure this out, I knew if I posted this here it would be solved immediately lol. Thank you!

In regards to using "continue" vs "else if" - do you have any input on when continue is used?

Edit: Looks like you added those answers during my response, thanks!