-1

This is the code that diminish intervals between stages in the game Life.

 document.getElementById("intervalMinus").addEventListener("click",function() {
    var intervalToMinus = +document.getElementById("labelInterval").innerHTML - 100;
    (intervalToMinus < 100) ? intervalToMinus += 90 : intervalToMinus;
    (intervalToMinus <= 0) ? intervalToMinus == 10 : intervalToMinus;
    document.getElementById("labelInterval").innerHTML = intervalToMinus;
    changeInterval();
  });

Obviosly when you tap the button it reduces the interval by 100, but if the interval is already less then 100, by 10.

(intervalToMinus > 0) ? intervalToMinus == 10 : intervalToMinus;

This part should guarantee that interval will never become negative. But it doesnt't work! I checked in chrome debugger, and saw, that JS just ignore this particular line.
intervalToMinus is equal to 0. Condition is true. Doesn't work. Why?

DeuS7
  • 1
  • It's a typo, you have `==` instead of `+=`. Voting to close as typo/non-repro. – T.J. Crowder Aug 05 '17 at 13:34
  • 1
    **Strongly** recommend using `if`, or at least `&&`. This is a straight-out abuse of the conditional operator. A nice simple `if/else if/else` would be dramatically easier to read and understand as well. – T.J. Crowder Aug 05 '17 at 13:35
  • *"...but if the interval is already less then 100..."* No, if the **resulting** interval, after you subtract 100, is `< 100`. Very different thing. – T.J. Crowder Aug 05 '17 at 13:36
  • [Apparently](https://stackoverflow.com/questions/45522506/why-does-js-disregard-the-condition-and-act-on-his-own#comment78005201_45522532), this is a duplicate of [*Why doesn't my equality comparison using = (a single equals) work correctly?*](http://stackoverflow.com/questions/38268329/). – T.J. Crowder Aug 05 '17 at 13:38

1 Answers1

0

You are not assigning a new value, just testing for equality here:

(intervalToMinus <= 0) ? intervalToMinus == 10 : intervalToMinus;

What you want is probably this:

(intervalToMinus <= 0) ? intervalToMinus = 10 : intervalToMinus;

Notice the single '=' instead of "==".

Martin
  • 7,634
  • 1
  • 20
  • 23