Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript for performing an average calculation

    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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    FL, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    CYWebmaster.com - See why we dot com!!
    ACJavascripts.com - Cut & Paste Javascripts!
    SimplyProgram.com - Personal Blog

  • #3
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I'm doing it differently now but I'm still stuck

    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>

  • #4
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    And that script doesn't work?
    Shawn

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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>

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try this

    Code:
    <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>

  • #7
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Thank you all for the help!!!

    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!

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 :
    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.")

    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

  • #9
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's a great idea! Thanks for your help!!!!!

  • #10
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    error checking

    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

  • #11
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
     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.
    - Saj

  • #12
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you! I'll give it a try!

  • #13
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #14
    New to the CF scene
    Join Date
    Jun 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool! Thanks! I'll give that a try also!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •