1

I have written a web page in JavaScript that calculates your BMI and it seems to work but when I hit the calculate button an alert box pops up and says "your BMI is NaN". I know NaN stands for not a number. I just want to know why I can't get this to work.

<!DOCTYPE html>

<html>
<head>

    <title>BMI</title>

</head>

<body>
    <p>Your weight:<input type="number" id="value1" /></p>

    <p>Your height (in inches):<input type="number" id="value2" /></p>

<script>

    function calc() {
    var data1 = document.getElementById("value1");
    var data2 = document.getElementById("value2");
    var weight = (data1 * 703);
    var height = (weight) / (data2 * data2);
    alert("Your BMI is " + height);
    };

    </script>

    <button onclick="calc()">Calculate</button>

</body>
</html>
Flimzy
  • 68,325
  • 15
  • 126
  • 165
Adam Jones
  • 13
  • 2

2 Answers2

0

You have to get the innervalue of your data1 and data2.

<!DOCTYPE html>

<html>
<head>

    <title>BMI</title>

</head>

<body>
    <p>Your weight:<input type="number" id="value1" /></p>

    <p>Your height (in inches):<input type="number" id="value2" /></p>

<script>

    function calc() {
    var data1 = parseFloat(document.getElementById("value1").value);
    var data2 = parseFloat(document.getElementById("value2").value);
    var weight = (data1 * 703);
    var height = (weight) / (data2 * data2);
    alert("Your BMI is " + height);
    };

    </script>

    <button onclick="calc()">Calculate</button>

</body>
</html>
mehulmpt
  • 14,731
  • 12
  • 43
  • 83
0

Here's a fiddle: http://jsfiddle.net/wYfq8/

function calc() {
    var data1 = document.getElementById("value1").value;
    var data2 = document.getElementById("value2").value;
    var weight = (data1 * 703);
    var height = (weight) / (data2 * data2);
    alert("Your BMI is " + height);
};
Brad
  • 15,044
  • 6
  • 35
  • 54