Arkius Azure
Arkius Azure

Reputation: 179

Multiple Conditions in an If statement

if (gold >= 5 || (item1 = false) ) { 
  item1 = true;
  gold = gold - 5;
}

This will keep going even if item1 is true. I'd like for the function to not run if either of those are not met. Thank you

Upvotes: 1

Views: 90

Answers (3)

Fumarja
Fumarja

Reputation: 137

Try to change this:

if (gold >= 5 || (item1 = false) ) { item1 = true; gold = gold - 5; }

With this:

if (gold >= 5 || (item1 == false) ) { item1 = true; gold = gold - 5; }

With 2 = signs you check the equivalence. With 1 = sign you are assigning the variabile, noto comparing it. Hope this helps

Upvotes: 1

Terry
Terry

Reputation: 66093

You have to understand the difference between assignment (=) and equivalence comparison (==). Your code is assigning the value of false to the variable item1. To evaluate the value of item1, use:

if (gold >= 5 || item1 == false ) {
    item1 = true;
    gold = gold - 5;
}

Also, the additional nesting within parentheses is not necessary.

Upvotes: 2

Lucas
Lucas

Reputation: 173

Item1 = false is an assignment which will always evaluate as false.

You need item1 == false

Upvotes: 5

Related Questions