mercredi 9 juin 2021

Trying to make a BMI calculator in HTML but the Inperial units aren't working

I have tried to switch around my measurements but its still giving me wack answers. Was wondering if anything in my code was wrong. Im not sure why and i dont have any errors. All its doing is saying that the BMI is really high after the calculations. Also this is my first stack overflow post so if i get something wrong, please tell me.

  <html>
       <head>
    <title>BMI Calculator</title>
        <h4> im not sure why but the inperial is broken for this, i cant find out why and neither can Stack Overflow. </h4>
    <script type="text/javascript">
      
        function computeBMI() {
            var height = Number(document.getElementById("height").value);
            var heightunits = document.getElementById("heightunits").value;
            var weight = Number(document.getElementById("weight").value);
            var weightunits = document.getElementById("weightunits").value;
    
    
            //Convert all units to metric
            if (heightunits == "inches") height /= 3.93700787;
            if (weightunits == "lb") weight /= 2.20462;
    
            //Perform calculation
    
            //        var BMI = weight /Math.pow(height, 2)*10000;
            var BMI = Math.round(weight / Math.pow(height, 2) * 10000);
    
            //Display result of calculation
            document.getElementById("output").innerText = Math.round(BMI * 100) / 100;
    
            var output = Math.round(BMI * 100) / 100
            if (output < 18.5)
                document.getElementById("comment").innerText = "Underweight";
            else if (output >= 18.5 && output <= 25)
                document.getElementById("comment").innerText = "Normal";
            else if (output >= 25 && output <= 30)
                document.getElementById("comment").innerText = "Obese";
            else if (output > 30)
                document.getElementById("comment").innerText = "Overweight";
            // document.getElementById("answer").value = output; 
        }
    </script>
    </head>
     <body>
    <h1>Body Mass Index Calculator</h1>
    <p>Enter your height: <input type="text" id="height"/>
        <select type="multiple" id="heightunits">
            <option value="metres" selected="selected">metres</option>
            <option value="inches">inches</option>
        </select>
         </p>
    <p>Enter your weight: <input type="text" id="weight"/>
        <select type="multiple" id="weightunits">
            <option value="kg" selected="selected">kilograms</option>
            <option value="lb">pounds</option>
        </select>
    </p>
    <input type="submit" value="computeBMI" onclick="computeBMI();">
    <h1>Your BMI is: <span id="output">?</span></h1>
    
    <h2>This means you are: <span id="comment"> ?</span> </h2> 
     </body>
    </html>



Aucun commentaire:

Enregistrer un commentaire