View Full Version : bmi calculator with javascript

11-16-2011, 12:19 PM
I need help trying to figure out how to get my bmi calculator to work. I cant figure out what is wrong with my formula part and why it wont calculate the numbers you type in...pls help!

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<script type="text/javascript">

function calculateBMI()
var weight = numb(document.data.weight.value)
var height = numb(document.data.height.value)
var height2 = height2 * height2
var BMI = weight * 703 / (height2 * height2)



<style type="text/css">
div.c2 {text-align: center}
input.c1 {font-size: 8pt}
<div class="c2">
<form name="form" id="form"><input type="text" name="weight" size="4"> Weight (in Kilos) <input type="text" name="height" size="4"> Height (in Centimeters)<br>
<input type="text" name="BodyMassIndex" id="BodyMassIndex" size="4"> BMI <input type="button" class="c1" value="Calculate" onclick="calculateBMI()" name="button"> <input type="reset" class="c1" value="Clear Form"></form>

11-16-2011, 01:18 PM
Question 1: Where did you get that formula from? BMI usually is (weight in kg)/(height in cm)^2 and you are (trying) to calculate (weight in kg) * 703 / (height in cm)^4

Question 2: You are using a function numb() which is defined nowhere. Where did you get it from? Usually the native Javascript function for that purpose is called Number()

Question 3: What is document.data? I only see document.form in your HTML

Something like that will calculate the BMI (if weight and height are given correctly)

function calculateBMI()
var weight = Number(document.form.weight.value);
var height = Number(document.form.height.value);
var BMI = weight / (height * height);