mardi 17 septembre 2019

Ternary Operator - 3 conditions

I'd like to rewrite this with a ternary operator. I believe I need 2 operators.

if (a.quantity > b.quantity) {
      return -1;
  } else if (a.quantity < b.quantity) {
      return 1;
  } else {
      return 0;
  }

Ternary

return (a.quantity > b.quantity) ? -1 : (a.quantity < b.quantity) ? 1 : 0;

would this be the equivalent?

Aucun commentaire:

Enregistrer un commentaire