2017-11-15 18 views
0

私はJavaScriptを学ぶには非常に新しいですので、私はいくつかのデータを分類するために使用AN /他の場合はしましたが、データ量が膨大です。このif/else文をどのように簡略化しますか?

それは人の最大酸素摂取量をうまくいくし、その値が優秀であれば貧しい人々にダウン、決定VO2マックス電卓の一部です。

値を移動するための方法であるかもしれないから引き出されたデータテーブル?しかし、私は方法を知らない: 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'; 
      } 
     } 
+0

これは良い習慣と見なされるかどうかわかりません。整数のビット単位の計算を調べることができます。 'if ... else'ステートメントがたくさんある場所では、配列を作り、それぞれのカテゴリに対して' true'または 'false'を押します。次に、配列要素をビット単位でマスクし、1つの整数にシフトします。次に、ビット単位のマスキングを行い、 'switch ... case'(またはそれらの倍数)を使用して、どれが最適かを判断します。最初は複雑になるかもしれませんが、十分柔軟です。 –

答えて

2

各オプションに印刷する値のオブジェクトを作成します。ブールチェックの方法に基づいて、キーがカットオフであるオブジェクトを列挙できます。その後、あなたは一致した後にループのうち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) + ")"; 
関連する問題