dimanche 5 juillet 2015

Min of a couple of values which must not be 0

I need the minimum of a, b, c and d. But when one or more of them is 0 they fall out of the comparison. I could solve this with an if/else cascade but that is kind of ugly. Is there a cleaner way to solve this? Math.min alone doesn't work because it will result in a 0 when ever one of the variables is a 0.

Can I put them in an array and drop all the 0 in that array? Is there a Math.min for arrays?

bestPrice: Ember.computed('a', 'b', 'c', 'd', function() {
  var a = this.get('a');
  var b = this.get('b');
  var c = this.get('c');
  var d = this.get('d');

  return Math.min(a, b, c, d);
}),

Example:

  • a = 0
  • b = 10
  • c = 20
  • d = 30

bestPrice: 10




Aucun commentaire:

Enregistrer un commentaire