簡體   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