samedi 26 décembre 2020

JavaScript If-else range check in opposite not working

Can anyone please explain the logic behind this code?

function getGrade(score) {
    let grade;
    if (20 < score <= 30) {
      grade = "A";
    } else if (10 < score <= 20) {
      grade = "B";
    } else if (0 < score <= 10) {
      grade = "C";
    }
    return grade;
  }

  console.log(getGrade(15));    //'A'
  console.log(getGrade(4));     //'A'
  console.log(getGrade(20));    //'A'

It is working fine with using && operator in if-else or using opposite as: 30>= score > 25. Thanks in Advance.

Aucun commentaire:

Enregistrer un commentaire