你会如何简化这个if / else语句?

时间:2017-11-15 03:02:22

标签: javascript if-statement

我是学习JavaScript的新手,因此我使用if / else对某些数据进行分类,但数据量巨大。

它是VO2 Max计算器的一部分,它可以计算出人体VO2max,然后判断该值是否为优秀,直至差。

从中提取值的数据表可能是一种方法吗?但我不知道如何: Table of data

我怎么能简化这个?我能读到什么才能搞清楚?或者使用以下是不是不好的做法?感谢。

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';
            }
        }

1 个答案:

答案 0 :(得分:3)

创建您希望每个选项打印出来的值的对象。根据布尔检查的方式,您可以枚举键是截止值的对象。然后,在得到匹配后,你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) + ")";