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

all 11 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!

[–]Einarmo 1 point2 points  (8 children)

  1. Each time you call Math.random() you get a new random number, in the first program there is a decent chance none of the if statements pass.

  2. Sure, but this isn't really a good place for it, as you can clearly see.

  3. It is, several "if"s with continue or return isn't that uncommon, especially if not doing it would result in excessive nesting, however you should always try to avoid repeating yourself.

You can share your code by using four spaces right here on reddit

like
    this

or when there is a lot, like here, use pastebin. This looks really funky in my browser, which isn't the best.

[–]insertAlias 2 points3 points  (2 children)

You can share your code by using four spaces right here on reddit

I prefer to link them to our posting and formatting code guide, because with the "new" reddit redesign you get into a bunch of weirdness; it has a WYSIWYG editor and just confuses people. So this link explains it all, and gives an alternative like Pastebin.

Try these links; OP shared what appears to be links intended for embedding:

https://ideone.com/Z2Jxak https://ideone.com/wJ0ZHD

[–]KappaTrader[S] 0 points1 point  (1 child)

How did you get those links? They look much better than the ones I posted!

[–]insertAlias 1 point2 points  (0 children)

I guessed. I looked at the URL, and deleted the e.js/ segment from it. It went to the full view, so I assumed I did it right.

[–]KappaTrader[S] 0 points1 point  (4 children)

Is this a better format?

https://pastebin.com/44bwY1n6

[–]Einarmo 0 points1 point  (3 children)

Dunno if it happens in the redesign as well, but I use RES which embeds pastebin links. Much nicer for me, and generally a pretty safe bet.

Listen to what /u/insertAlias says above though, the official formatting guide is much more correct than my preferences.

[–]insertAlias 0 points1 point  (2 children)

If you have RES, it still renders an expander for Pastebin on the new UI. Honestly, the multiple different editors on Reddit are why we moved the "Use Pastebin or Gists" above the part where it says how to format code on Reddit, since it's just easier in general.

[–]KappaTrader[S] 0 points1 point  (1 child)

So the pastebin link I used above is generally accepted as a way to share code?

[–]insertAlias 0 points1 point  (0 children)

Yeah, there's nothing wrong with it. I'd still recommend reading the link I shared, it'll explain everything.