mardi 14 novembre 2017

How would you simplify this if/else statement?

I'm very new to learning JavaScript, so I've used an if/else to categorise some data, but the amount of data is huge.

It's part of a VO2 Max calculator that works out a persons VO2max and then decides if that value is Excellent, down to poor.

The data tables which the values are pulled from might be a way to go? But I don't know how: Table of data

How could I simplify this? What can I read to figure it out? Or is it not bad practice to use the below? Thanks.

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

Aucun commentaire:

Enregistrer un commentaire