lundi 21 juin 2021

Simplify nested if else statement in javascript [closed]

I'm currently doing a body mass index calculation using html css and javascipt (using jquery library). In javascript or other programming language, how can I simplify multiple nested if else statement? The code works well, but I'm curious about another efficient method (maybe array or list?) to reduce working.

if (age < 18){

   if (age == 9){
       if (bmi < 14){
           $(".Classification").text(underweight)
       }
       else if (bmi < 18.6){
           $(".Classification").text(healthy)
       }
       else if (bmi < 21){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 21){
           $(".Classification").text(obese)
       }
   }
   else if (age == 10){
       if (bmi < 14.2){
           $(".Classification").text(underweight)
       }
       else if (bmi < 19.4){
           $(".Classification").text(healthy)
       }
       else if (bmi < 22.2){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 22.2){
           $(".Classification").text(obese)
       }
   }
   else if (age == 11){
       if (bmi < 14.6){
           $(".Classification").text(underweight)
       }
       else if (bmi < 20.2){
           $(".Classification").text(healthy)
       }
       else if (bmi < 23.2){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 23.2){
           $(".Classification").text(obese)
       }
   }
   else if (age == 12){
       if (bmi < 15){
           $(".Classification").text(underweight)
       }
       else if (bmi < 21){
           $(".Classification").text(healthy)
       }
       else if (bmi < 24.2){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 24.2){
           $(".Classification").text(obese)
       }
   }
   else if (age == 13){
       if (bmi < 15.4){
           $(".Classification").text(underweight)
       }
       else if (bmi < 21.8){
           $(".Classification").text(healthy)
       }
       else if (bmi < 25.2){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 25.2){
           $(".Classification").text(obese)
       }
   }
   else if (age == 14){
       if (bmi < 16){
           $(".Classification").text(underweight)
       }
       else if (bmi < 22.6){
           $(".Classification").text(healthy)
       }
       else if (bmi < 26){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 26){
           $(".Classification").text(obese)
       }
   }
   else if (age == 15){
       if (bmi < 16.6){
           $(".Classification").text(underweight)
       }
       else if (bmi < 23.4){
           $(".Classification").text(healthy)
       }
       else if (bmi < 26.8){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 26.8){
           $(".Classification").text(obese)
       }
   }
   else if (age == 16){
       if (bmi < 17.2){
           $(".Classification").text(underweight)
       }
       else if (bmi < 24.2){
           $(".Classification").text(healthy)
       }
       else if (bmi < 27.6){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 27.6){
           $(".Classification").text(obese)
       }
   }
   
   else if (age == 17){
       if (bmi < 17.8){
           $(".Classification").text(underweight)
       }
       else if (bmi < 24.8){
           $(".Classification").text(healthy)
       }
       else if (bmi < 28.2){
           $(".Classification").text(overweight)
       }
       else if (bmi >= 28.2){
           $(".Classification").text(obese)
       }
   }
   else{
       $(".Classification").text("age out of range")
   }


} else if (age >= 18) {

   if (bmi < 18.5){
       $(".Classification").text(underweight)
   }
   else if (bmi < 25){
       $(".Classification").text(healthy)
   }
   else if (bmi < 30){
       $(".Classification").text(overweight)
   }
   else if (bmi < 35){
       $(".Classification").text(obeseI)
   }
   else if (bmi < 40){
       $(".Classification").text(obeseII)
   }
   else if (bmi >= 40){
       $(".Classification").text(obeseIII)
   }

} else {
   $(".Classification").text("null")
}

Aucun commentaire:

Enregistrer un commentaire