dimanche 19 mai 2019

Clarification about if statements

Got that code:

function orderMyLogic(val) {
  if (val < 10) {
    return "Less than 10";
  } else if (val < 5) {
    return "Less than 5";
  } else {
    return "Greater than or equal to 10";
  }
}


console.log(orderMyLogic(7));
console.log(orderMyLogic(4));
console.log(orderMyLogic(11));

So the result is

Less than 10
Less than 10
Greater than or equal to 10

I understand why this is the result, but what options in syntax ( if there is ) that i can take so their outcome will be the same as in the code below without changing the places of ( val < 10 ) and ( val < 5 )

function orderMyLogic(val) {
  if (val < 5) {
    return "Less than 5";
  } else if (val < 10) {
    return "Less than 10";
  } else {
    return "Greater than or equal to 10";
  }
}

console.log(orderMyLogic(7));
console.log(orderMyLogic(4));
console.log(orderMyLogic(11));

that the result will be:

Less than 10
Less than 5
Greater than or equal to 10

Aucun commentaire:

Enregistrer un commentaire