Click to See Complete Forum and Search --> : BIM generator


shipwreck99
August 17th, 2007, 03:42 AM
i'm stuck on a javascript which is supposed to be getting the BIM(bodyy mass index) in a webpage. can anyone help..?how do i post the code that i've cam up with?


<html>
<head>
<script type="text/javascript">
function bim(weight,height)
{
var bm=weight/height^2;
return bm;
}
</script>
</head>
<body>

<script type="text/javascript">

var age =prompt("Please Enter your age", "");
if (age<18)
document.write("You're not authorized to view the resources");
else if (age>=18)
var w,h;
w =prompt("what's your weight in KG?");
h =prompt("What's your height in cm?");
document.write("your bim is" + bim()+ <br/>);



</script>


</body>
</html>

andreasblixt
August 17th, 2007, 05:34 AM
Put your code within [code] tags.

As for your problem, "stuck" is not a good description of it. Please describe your error in detail (unexpected output, script error, ...)

I recommend you try your script in Firefox as it has a JavaScript console which gives you detailed script errors if something's wrong.

Now it just so happens that I can see that you're getting the wrong result because you're using the XOR operator, ^. You intended to increase the height by a power of 2, which is achieved by using Math.pow(x, y) where x is the number and y is the power.

P.S. it's BMI (Body Mass Index, which you actually wrote in your post), not "BIM".

shipwreck99
August 17th, 2007, 06:16 AM
ok i've got to the stage where i can get the BMI but it's coming in 10-12 decimal places. how can i get it to show the nearest full number?
thanks

<html>
<head>
<script type="text/javascript">
function bmi()
{
var w,h;
w =prompt("what's your weight in KG?");
h =prompt("What's your height in metres?");
var bm=w/(h*h);
return bm;
}
</script>
</head>
<body>

<script type="text/javascript">

var age =prompt("Please Enter your age", "");
if (age<18)
document.write("You're not authorized to view the resources");
else if (age>=18)
document.write("your bmi is" +""+ bmi()+ <br/>);



</script>

andreasblixt
August 17th, 2007, 07:11 AM
Here you go; the complete set of methods in the Math object: http://www.w3schools.com/jsref/jsref_obj_math.asp

Of particular interest to you is the Math.round(x) method.

shipwreck99
August 17th, 2007, 08:30 AM
thanks a lot....one more thing. i need to change the background colour where the java script text going to show...& also the text colour..I'm using an internal css style sheet just for this page. but how can change the background colour or text colour of the texts that will come up from the script...

PeejAvery
August 17th, 2007, 11:33 AM
document.getElementById('id').style.background = '#3773b3';

or

formname.elementname.style.color = '#3773b3';

shipwreck99
August 17th, 2007, 09:32 PM
i stil couldn't get it to work. Where should the code(for changed background & text color) will be placed? in the function or in the body?
i defined the variables w & h in the function but i want their value to show in the webpage but i couldn't do it

<h1>Body Mass Index(BMI) calculator</h1>
<p>Your Height = "h"</p> //the h is defined in the function

<p> Your Weight = "w"</p>

andreasblixt
August 20th, 2007, 02:58 AM
You could use the following HTML:
<h1>Body Mass Index (BMI) calculator</h1>
<p>Your height is <span id="height">?</span> metres.</p>
<p>Your weight is <span id="weight">?</span> kilograms.</p>

And the following JavaScript:
document.getElementById("height").innerHTML = h;
document.getElementById("weight").innerHTML = w;