简化javascript中的嵌套if else语句

时间:2021-06-21 15:08:15

标签: javascript jquery arrays list if-statement

我目前正在使用 html css 和 javascipt(使用 jquery 库)进行体重指数计算。在javascript或其他编程语言中,如何简化多个嵌套的if else语句?代码运行良好,但我很好奇另一种有效的方法(可能是数组或列表?)来减少工作。

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")
}

2 个答案:

答案 0 :(得分:1)

简化/美化 if 语句的一种方法是使用 switch 语句

对于 JavaScript:https://www.w3schools.com/js/js_switch.asp

答案 1 :(得分:1)

如果你提供一个带有“决策点”的嵌套数组,它可以被简化很多:

function bmi(age,b) {
  const text=["underweight","healthy","overweight","obese","obese II","obese III"],
        o={9:[14,   18.6, 21          ],
          10:[14.2, 19.4, 22.2        ],
          11:[14.6, 20.2, 23.2        ],
          12:[15,   21,   24.2        ],
          13:[15.4, 21.8, 25.2        ],
          14:[16,   22.6, 26          ],
          15:[16.6, 23.4, 26.8        ],
          16:[17.2, 24.2, 27.6        ],
          17:[17.8, 24.8, 28.2        ],
          18:[18.5, 25,   30,  35,  40]};
  let sc=o[Math.min(age,18)];
  if (!sc) return "too young for BMI";
  let i=sc.findIndex(v=>v>b);
  if (i==-1) i=sc.length
  return text[i]
}
const tests=[[8,18],[12,23],[13,25],[14,26],[15,27],[16,16],[17,22],[22,42]];
tests.forEach(([age,b])=>console.log(age+"y, bmi="+b+" : "+bmi(age,b)))