繁体   English   中英

您如何简化该if / else语句?

[英]How would you simplify this if/else statement?

我对学习JavaScript非常陌生,因此我使用了if / else来对一些数据进行分类,但是数据量很大。

它是VO2 Max计算器的一部分,该计算器计算出一个人的VO2max,然后确定该值是否为“优”(低至差)。

从中提取值的数据表可能是一种方法? 但是我不知道怎么做: 数据表

我该如何简化? 我可以阅读以找出答案吗? 还是使用以下内容不是不好的做法? 谢谢。

if(vitals.gender === 1) {
            if(vitals.age >= 18 && vitals.age <= 25) {
                if(vo2 > 60) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 52) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 47) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 42) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 37) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 30) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else if (vitals.age >= 26 && vitals.age <= 35) {
                 if(vo2 > 56) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 49) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 43) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 40) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 35) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 30) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else if (vitals.age >= 36 && vitals.age <= 45) {
                 if(vo2 > 51) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 43) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 39) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 35) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 31) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 26) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else if (vitals.age >= 46 && vitals.age <= 55) {
                 if(vo2 > 45) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 39) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 36) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 32) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 29) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 25) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } 
            } else if (vitals.age >= 56 && vitals.age <= 65) {
                 if(vo2 > 41) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 36) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 32) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 30) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 26) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 22) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else if (vitals.age >= 65) {
                 if(vo2 > 37) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 33) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 29) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 26) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 22) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 20) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else {
                VO2MaxRating = "Missing";
                document.getElementById("outputVo2").textContent = 'Either you\re under 18 or missing details';
            }
        } else {
            if(vitals.age >= 18 && vitals.age <= 25) {
                if(vo2 > 56) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 47) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 42) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 38) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 33) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 28) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else if (vitals.age >= 26 && vitals.age <= 35) {
                 if(vo2 > 52) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 45) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 39) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 35) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 31) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 26) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else if (vitals.age >= 36 && vitals.age <= 45) {
                 if(vo2 > 45) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 38) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 34) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 31) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 27) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 22) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else if (vitals.age >= 46 && vitals.age <= 55) {
                 if(vo2 > 40) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 34) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 31) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 28) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 25) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 20) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } 
            } else if (vitals.age >= 56 && vitals.age <= 65) {
                 if(vo2 > 37) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 32) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 28) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 25) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 22) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 18) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else if (vitals.age >= 65) {
                 if(vo2 > 32) {
                    VO2MaxRating = "Excellent";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 28) {
                    VO2MaxRating = "Good";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 25) {
                    VO2MaxRating = "Above Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 22) {
                    VO2MaxRating = "Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 19) {
                    VO2MaxRating = "Below Average";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else if (vo2 >= 17) {
                    VO2MaxRating = "Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                } else {
                    VO2MaxRating = "Very Poor";
                    document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + VO2MaxRating + ")";
                }
            } else {
                VO2MaxRating = 
                document.getElementById("outputVo2").textContent = 'Either you\re under 18 or missing details';
            }
        }

使您要打印每个选项的值成为对象。 根据布尔检查的方式,您可以枚举键为截止值的对象。 然后,你break了循环,你得到一个比赛后。

编辑:如果将循环转换为函数,则可重用。 只需传入您的vo2和值的对象即可。

const VO2MaxRating = {
  52: 'Excellent',
  47: 'Good',
  42: 'Above Average',
  37: 'Average',
  30: 'Below Average',
  0: 'Poor',
}

const setRating = (rate, maxRatingObject) => {
  for (let vo2Rating in maxRatingObject) {
    if (rate >= parseInt(vo2Rating)) {
      return maxRatingObject[vo2Rating]
    }
  }
}
document.getElementById("outputVo2").textContent = vo2 + ' ml/kg/min (' + setRating(vo2, VO2MaxRating) + ")";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM