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>
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Adam Jones
  • 11
  • 3

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
  • 15,861
  • 12
  • 48
  • 88
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,361
  • 6
  • 36
  • 57