0

My code here

if (parent4 && parent5 && parent6 && (_state[tree][parent4]) + (_state[tree][parent5]) + (_state[tree][parent6]) !== 8) {
  return false;
} else {
  return true;
}

It works, and has some of the elements I want, namely that if the sum of the three _states !== 8, it returns false.

I'll try to explain the logic I'm trying to achieve as simply as I can:

if par4 + par5 + par6 !>= 8
  return false

if par4 + par5 !>= 8
  return false

if par4 OR par5 OR par6 !>= 8
  return false

else
  return true

I abbreviated the code for simplicity and ease of understanding.

Swapping around return false and return true and changing it to >= doesn't work, because for all elements, even ones I don't mention here, it needs to go to return true by default.

7
  • Entire expression in single parentheses -> (_state[tree][parent4] + _state[tree][parent5] + _state[tree][parent6]) !== 8 Commented Mar 27, 2020 at 16:24
  • 1
    greater than or not equal to???? so would just be > Commented Mar 27, 2020 at 16:28
  • I'll rephrase sorry, not greater than or equal to so to write it out !>= 8 it should return false, else return true. Commented Mar 27, 2020 at 16:34
  • if par4 + par5 !>= 8 what would !>= 8 mean? "Not greater or equal to"? Because that is "less than". If it's "not equal AND greater or equal" then that's just "greater than". Commented Mar 27, 2020 at 16:34
  • That makes some sense, however replacing the "!==" with "greater than" doesn't seem to work. Then it returns false whenever the sum is "greater than" I need it return true whenever the sum is "greater than" and in all other cases. Commented Mar 27, 2020 at 16:39

1 Answer 1

1

How about this?

if (parent4 && parent5 && parent6) {
    if ((_state[tree][parent4] + _state[tree][parent4] + _state[tree][parent4]) !== 8) {
        return false;
    } else {
        return true;
    }
}

Or in a one-liner:

if (parent4 && parent5 && parent6 && (_state[tree][parent4] + _state[tree][parent4] + _state[tree][parent4]) !== 8) {
    return false;
} else {
    return true;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't the oneliner what OP already has but with the brackets placed differently (which wouldn't affect the logic)?
You're right. I wouldn't think it should affect anything. Actually not sure why the OP code isn't working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.