0

I am creating a game of chess and have this line of code:

void (!selectedPiece.moved && selectedPiece.moved = true);

What it does, is check if selectedPiece.moved is false, and if it is, continue and set selectedPiece.moved to true.

As @Klaycon said, it reads it like this:

void (!selectedPiece.moved && selectedPiece.moved) = true;

And that is when I get an invalid left-hand side assignment. I know I could replace it with an if statement but I was wondering if there was something else I could do to fix it.

1
  • 1
    To clarify, the problem is not JavaScript evaluating your properties as you write, it's simple order of operations. Currently it evaluates as if you wrote (!selectedPiece.moved && selectedPiece.moved) = true, hence the "invalid left-hand side assignment". Commented Jul 16, 2020 at 19:18

1 Answer 1

1

Just add some parenthesis. The outer ones are not necessary.

void !selectedPiece.moved && (selectedPiece.moved = true);

An even shorter approach uses an logical OR without a logical NOT for the first part.

void selectedPiece.moved || (selectedPiece.moved = true);

But anyway, you could assign true without check, if the value is a boolean.

selectedPiece.moved = true;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.