jeudi 21 novembre 2019

Javascript - update var with simplified oneline if statement

Small question, I have a var in JS and I want to increment it as long as the current value is under 99, else it should be reset to zero.

current simplified code

var age = 35;
age = ( age < 99) ? age+1 : 0;

Now, this is reasonably human readable. But what if my var would be obj[layer01][gridY][gridX][shape] That would change my line to something like:

obj[layer01][gridY][gridX][shape] = ( obj[layer01][gridY][gridX][shape] < 99) ? obj[layer01][gridY][gridX][shape]+1 : 0;

The question

Is there a way to simplyfie this? Something like:

age = (< 99) ? ++ : 0;

Aucun commentaire:

Enregistrer un commentaire