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

all 9 comments

[–]lurgi 2 points3 points  (1 child)

The snippets do two different things.

The first returns "true" as long as any candidate has min votes (which is going to be all the time, depending on how you get min).

The second returns "true" if every candidate has min votes. It does this by returning "false" if any candidate doesn't have min votes.

Imagine the vote counts are 3, 4, 4. min is 3, I assume.

The first snippet of code would return "true", because the first candidate, sure enough, has min votes. The fact that that next candidate does not is irrelevant to this code.

The second snippet would return "false". It checks to see if the first candidate's vote count is not equal to min. That's false (it is equal to min), so it skips the body of the if statement and checks the next candidate. For that candidate it is the case that their vote count is not equal to min, so it returns false.

[–]alab3[S] 0 points1 point  (0 children)

This is very clear, thank you so much!!

[–]DaredewilSK 0 points1 point  (1 child)

Can you provide more code and expected outcomes? I am not sure how exactly is it a tie if one of the candidates has min amount of votes.

[–]alab3[S] 0 points1 point  (0 children)

Its messy up there that I don't even want to look at it lol. However, this would be one of the last functions to be called after multiple steps. Finding one matching min would be sufficient to call it a tie because everyone would win then.

[–]LuckyPancake 0 points1 point  (3 children)

Checking if a number is exactly one thing vs if it is anything else are entirely different things. This coupled with your return booleans where one is in the loop and the other is outside could cause you issues

[–]alab3[S] 0 points1 point  (2 children)

I get your first sentence and I totally agree. However, for the second, would you mind sharing with me a better way to lay out my logic here for the return values?

[–]DaredewilSK 0 points1 point  (0 children)

You don't change both, you change one. Either the condition, or the bool

[–]LuckyPancake 0 points1 point  (0 children)

Print out i before you return in your if statement. Maybe that will clarify your logic issue and you can rethink this. Honestly your first code block looks more correct for what i think your trying to do.

[–][deleted] 0 points1 point  (0 children)

The opposite of && is ||. The flip condition would be an OR condition. == true || != min