mercredi 23 mai 2018

Is there a way to reduce the if else cases?

let invert = moreIsBetter.includes(metric);

if (!invert) {
  if (count > parseInt(Min) && Min) {
    className = 'Class A';
  }

  if (count > parseInt(Max) && Max) {
    className = 'Class B';
  }
} else {
  if (count < parseInt(Min) && Min) {
    className = 'Class A';
  }

  if (count < parseInt(Max) && Max) {
    className = 'Class B';
  }
}

return className;

Is there a way to refactor this code further. I have tried adding the 'invert' condition check inside the if check itself. But still is there a better way than this ?

  if ((!invert && count > parseInt(Min)) || (invert && count < parseInt(Min)) && Min) {
    className = 'Class A';
  }


  if ((!invert && count > parseInt(Max)) || (invert && count < parseInt(Max)) && Max) {
    className = 'Class B';
  }

Aucun commentaire:

Enregistrer un commentaire