Results 1 to 8 of 8

Thread: javascript help please.need to work out average

  1. #1
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default javascript help please.need to work out average

    Hi

    Iam new here and new to javescript

    ok my problem is i have to try and find out the average of some plant (don't ask )
    i have tried everything but still no good, i am sur eit is simple but i am blond


    anyways here is what i have done so far any help would be great



    Code:
    <HTML>
    <HEAD>
    <TITLE>
    Plant Experiments
    </TITLE>
    
    <SCRIPT LANGUAGE = "JavaScript">
     /* 
      * Program to calculate average height of plants.. 
      * 
      * 20/05/2009
      */
    
    //Experimental results of Table 1 stored in arrays.
    var plantHeights = [15,16,17,18,19];
    var plantNumbers = [2,1,6,4,2];
    
    //Part (ii).
    //Write code to declare and initialise new array to represent the third row of the table.
    var avg = new Array(5)
    var avg = ["60","80","187","180","114"] ;
    avg[0] = "60";
    avg[1] = "80";
    avg[2] = "187";
    avg[3] = "180";
    avg[4] = "114";
    
    //Part  (iv).
    //Write code to calculate the average height of the plants and write it out in the browser window.
    
    
    Average =  avg.length
    document.write ("The Average height is: " + Average + "<br>")
    
    
    </SCRIPT>
    
    </HEAD>
    <BODY>
    </BODY>
    </HTML>
    thanks for taking the time to read my problem and i hope someone can help.

    love
    kelly
    Last edited by smellykelly; 02-01-2010 at 12:52 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    To find average use this function:
    Code:
    Array.prototype.average = function(){
      var avg = 0;
      var old_this = this.length;
      while(this.length > 0){
        avg += this.shift();
      }
      return avg / old_this;
    };
    Usage:
    Code:
    var a = [1, 3];
    return a.average();
    Last edited by Nile; 01-28-2010 at 03:20 AM.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    smellykelly (01-29-2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hi thanks for that
    But it appears that i have to use the for loop (method)
    i will continue to work on it but if anyone knows what i am babbling on about can shed some light on it i would be most greatful.

    thanks in advance
    kelly (some what dumbstruck)

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    So I'm doing you're homework?
    Code:
    <script type="text/javascript">
    Array.prototype.average = function(){
      var avg = 0;
      for(var a = 0; a < this.length; a++){
        avg += this[a];
      }
      return avg / this.length;
    };
    var a = [1, 3];
    alert(a.average());
    </script>
    Jeremy | jfein.net

  6. The Following User Says Thank You to Nile For This Useful Post:

    smellykelly (01-29-2010)

  7. #5
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    So I'm doing you're homework?
    Code:
    <script type="text/javascript">
    Array.prototype.average = function(){
      var avg = 0;
      for(var a = 0; a < this.length; a++){
        avg += this[a];
      }
      return avg / this.length;
    };
    var a = [1, 3];
    alert(a.average());
    </script>
    You could say that but i am 32 and thick when it comes to javascript and it is the only bit of javascript in the course (phew)
    I have tried to do it for like the last 3 days

    Thanks for your help
    kelly

  8. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help you! Your welcome!

    It seems your topic is solved... Please set the status to resolved.. To do this:
    Go to your first post ->
    Edit your first post ->
    Click "Go Advanced" ->
    Then in the drop down next to the title, select "RESOLVED"
    Jeremy | jfein.net

  9. #7
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with same issue

    Hiya, I have a similar task to complete just with different numbers, I am trying to make sense of the code but really struggling with it all, please could someone help me? also I am not sure the average is working correctly in my documents from the code structure i used from your example??

  10. #8
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hi there

    i am havung the same problem but dont seem to be having any success
    would love some help please

    thanks in advance
    stumpped

Bookmarks

Posting Permissions

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