0
私はJavaScriptを学ぶには非常に新しいですので、私はいくつかのデータを分類するために使用AN /他の場合はしましたが、データ量が膨大です。このif/else文をどのように簡略化しますか?
それは人の最大酸素摂取量をうまくいくし、その値が優秀であれば貧しい人々にダウン、決定VO2マックス電卓の一部です。
値を移動するための方法であるかもしれないから引き出されたデータテーブル?しかし、私は方法を知らない:
私はこれを単純化することができますか?それを理解するために何を読むことができますか?それとも悪い習慣ではないのですか?ありがとう。
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';
}
}
これは良い習慣と見なされるかどうかわかりません。整数のビット単位の計算を調べることができます。 'if ... else'ステートメントがたくさんある場所では、配列を作り、それぞれのカテゴリに対して' true'または 'false'を押します。次に、配列要素をビット単位でマスクし、1つの整数にシフトします。次に、ビット単位のマスキングを行い、 'switch ... case'(またはそれらの倍数)を使用して、どれが最適かを判断します。最初は複雑になるかもしれませんが、十分柔軟です。 –