gpt4 book ai didi

javascript - 您将如何简化这个 if/else 语句?

转载 作者:行者123 更新时间:2023-11-28 17:43:48 27 4
gpt4 key购买 nike

我刚开始学习 JavaScript,所以我使用了 if/else 来对一些数据进行分类,但数据量很大。

它是最大摄氧量计算器的一部分,可计算出一个人的最大摄氧量,然后决定该值是优秀还是较差。

从数据表中提取值可能是一种方法?但我不知道如何: 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';
}
}

最佳答案

创建一个您希望每个选项打印的值的对象。根据 bool 检查的方式,您可以枚举键为截止点的对象。然后,在获得匹配项后打破循环。

编辑:如果将循环变成函数,它就可以重用。只需传入您的 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) + ")";

关于javascript - 您将如何简化这个 if/else 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47298654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com