Skip to content

JS: Can't easily do assignments in loop conditions #134

Open

Description

@nwinter

From the forum:

while (enemy=this.findNearestEnemy()) this.attack(enemy);

... gives a message like, "Fix Your Code: expected a conditional expression and instead saw an assignment". However, this workaround works:

while ((enemy=this.findNearestEnemy())) this.attack(enemy);

Haven't yet looked to see where in the process the assignment is being broken.

Activity

  1. UltCombo commented on Jun 30, 2015

    @UltCombo
    Contributor

    This is actually how JSHint is supposed to work.

    The extra set of parens around an assignment inside a conditional is the de-facto way to communicate to the linter/compiler: "I really intend to do an assignment here, don't bother me" since the olden C days.

  2. nwinter commented on Jun 30, 2015

    @nwinter
    ContributorAuthor

    Ah! Then we should probably rewrite the warning message in Aether so that it's less confusing; perhaps, "If you really want = (assignment) instead of == (equality), add parentheses: (enemy = this.findNearestEnemy())."

  3. UltCombo commented on Jun 30, 2015

    @UltCombo
    Contributor

    Makes sense. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions