Multiple Conditions in an If statement

86 Views Asked by At
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

3

There are 3 best solutions below

0
On

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

You need item1 == false

0
On

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.

0
On

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