View Full Version : JavaScript for performing an average calculation
janihenn
06-01-2003, 04:59 AM
I need to write a script to perform an average calculation. I'm asking for a person's gender, age, weight, and height and have to compare it to a table and give the average. This is for my online college Web class. I have had no success in finding out how to do this. Any help would be greatly appreciated.
Thank you!
Janice
ACJavascript
06-01-2003, 04:19 PM
What kind of averege.
Like if the average hieght and weight of a 18 year old is: bla bla ?
if so then your really just trying to find the average between the weight and hieght.. right?
janihenn
06-01-2003, 05:57 PM
with nesting IF's. This is all new to me so.....If you have time to look it over, that would be great. If not, I'll send it to my instructor.
If I pull out the coding at the ELSE statement pertaining to female, it works fine, although if I type in female at the gender
prompt, I jump to height for males. I've been trying to figure this out for 5 hours now and I'm stuck. It doesn't help that our textbook doesn't go into details.
Here the coding:
<html>
<head>
<title>JavaScript Lab 11</title>
</head>
<body>
<SCRIPT LANGUAGE = "JavaScript">
document.write("<H1><B>Get your Height and Weight Average</H1></B><BR>");
document.write("Your average calculation will be based on men and women aged 20 to 50.");
var gender
var gender = prompt("Please tell me your gender", "male or female");
if ( (gender == "male") || (gender == "Male") || (gender == "MALE") )
{
var weight = prompt("Please tell me your weight","number in pounds");
if( (weight>150) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
}
var height = prompt("Please tell me your height","number in inches");
if( (height>70) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")
else (( (gender == "female") || (gender == "Female") || (gender == "FEMALE") )
{
var weight = prompt("Please tell me your weight","number in pounds");
if( (weight>130) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
}
var height = prompt("Please tell me your height","number in inches");
if( (height>64) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")
</SCRIPT>
</BODY>
</HTML>
shlagish
06-01-2003, 06:09 PM
And that script doesn't work?
although if I type in female at the gender
prompt, I jump to height for males.
That is because you have male written in the female section.
Try this
<html>
<head>
<title>JavaScript Lab 11</title>
</head>
<body>
<SCRIPT LANGUAGE = "JavaScript">
document.write("<H1><B>Get your Height and Weight Average</H1></B><BR>");
document.write("Your average calculation will be based on men and women aged 20 to 50.");
var gender
var gender = prompt("Please tell me your gender", "male or female");
if ( (gender == "male") || (gender == "Male") || (gender == "MALE") ){
var weight = prompt("Please tell me your weight","number in pounds");
if( (weight>150) ){
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else{
alert("Your weight is below average for males between the ages of 20 and 50.")
}
var height = prompt("Please tell me your height","number in inches");
if( (height>70) ){
alert("Your height is above average for males between the ages of 20 and 50.")
}
else{
alert("Your height is below average for males between the ages of 20 and 50.")
}
}
if ( (gender == "female") || (gender == "Female") || (gender == "FEMALE") ){
var weight = prompt("Please tell me your weight","number in pounds");
if( (weight>130) ){
alert("Your weight is above average for female between the ages of 20 and 50.")
}
else{
alert("Your weight is below average for female between the ages of 20 and 50.")
}
var height = prompt("Please tell me your height","number in inches");
if( (height>64) ){
alert("Your height is above average for female between the ages of 20 and 50.")
}
else{
alert("Your height is below average for female between the ages of 20 and 50.")
}
}
</SCRIPT>
</BODY>
</HTML>
Garadon
06-01-2003, 06:22 PM
try this
<head>
<title>JavaScript Lab 11</title>
</head>
<body>
<SCRIPT LANGUAGE = "JavaScript">
document.write("<H1><B>Get your Height and Weight Average</H1></B><BR>");
document.write("Your average calculation will be based on men and women aged 20 to 50.");
var gender = prompt("Please tell me your gender", "male or female");
if (gender.toLowerCase()=="male")
{
Male();
}
if(gender.toLowerCase()== "female")
{
Female();
}
function Male()
{
var weight = prompt("Please tell me your weight","number in pounds");
if( (weight>150) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
var height = prompt("Please tell me your height","number in inches");
if( (height>70) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")
}
function Female()
{
var weight = prompt("Please tell me your weight","number in pounds");
if( (weight>130) )
{
alert("Your weight is above average for females between the ages of 20 and 50.")
}
else
alert("Your weight is below average for females between the ages of 20 and 50.")
var height = prompt("Please tell me your height","number in inches");
if( (height>64) )
{
alert("Your height is above average for females between the ages of 20 and 50.")
}
else
alert("Your height is below average for females between the ages of 20 and 50.")
}
</SCRIPT>
</BODY>
</HTML>
janihenn
06-01-2003, 06:29 PM
It works beautifully now! I'm just learning so this forum is great! Now, I'm going to compare the one that works with mine to see the difference so I'll know where I made my errors!
Thank you all for such quick replies! I'll be able to finish my homework assignment today!
Garadon
06-01-2003, 06:39 PM
just a suggestion if u want to make scripts in the future indent ur code it makes it a lot easier to find the fault u made :)
eaxmple ur code:
if ( (gender == "male") || (gender == "Male") || (gender == "MALE") )
{
var weight = prompt("Please tell me your weight","number in pounds");
if( (weight>150) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
}
var height = prompt("Please tell me your height","number in inches");
if( (height>70) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")
now with indents :
if ( (gender == "male") || (gender == "Male") || (gender == "MALE") )
{
var weight = prompt("Please tell me your weight","number in pounds");
if( (weight>150) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
}
var height = prompt("Please tell me your height","number in inches");
if( (height>70) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")
as u see if u had done this u could have seen that the if sentence where u check if the first promt i male ends to early
janihenn
06-01-2003, 06:52 PM
That's a great idea! Thanks for your help!!!!!
janihenn
06-01-2003, 08:55 PM
How could I put in code that if someone types anything other than what the prompt requests, they will get a prompt telling them what information needs to be typed?
Thanks for any suggestions!
Janice
Try this:
var weight = prompt("Please tell me your weight","number in pounds");
if (isNAN(weight) || weight == "") {
alert("Please enter a numerical value only.");
var weight = prompt("Please tell me your weight","number in pounds");
}
var gender = prompt("Please tell me your gender", "male or female");
if (gender != "male" || gender != "Male || gender != "MALE" || gender != "female" || gender != "Female || gender != "FEMALE")
alert("Please enter either male, Male, or MALE for Male, or female, Female or FEMALE for female");
var gender = prompt("Please tell me your gender", "male or female");
}
And something to that effect for the rest.
janihenn
06-01-2003, 09:30 PM
Thank you! I'll give it a try!
Garadon
06-01-2003, 09:40 PM
if u lower case gender as I did u can minimise ur if sentences, which in theory should make u script faster as it checks up on less
janihenn
06-02-2003, 02:58 AM
Cool! Thanks! I'll give that a try also!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.