Closed Thread
Results 1 to 4 of 4

Thread: need help on a javascript please (newbie)

  1. #1
    kelly12345 is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Red face need help on a javascript please (newbie)

    Hello codecall members

    I am very new to javascript as you can probably tell
    but i hope that someone can help me finish this off
    i need to find the average height via javascript.

    ok here is the maths side of it
    Total of height 60+80+187+180+114= 621
    Total of objects 4+5+11+10+6 = 36
    621/36= 17.25

    Average height is 17.25

    so i have messed about with this script for about a week now and all i have managed to do is get this on screen

    The average of the plants 60,80,187,180,114 is 124.2
    it isn't even the right answer and i do not need to list the array.string if thats what it is called lol just the total average.

    Please please can someone help as my boyfriend said i should give up before i even start

    I wish i have something to offer in return but all i have is my broken javascript

    thank you to you all

    love kelly
    Code:
    <HEAD>
    <TITLE>
    averages
    </TITLE>
    
    <SCRIPT LANGUAGE = "JavaScript">
     
    
    //Experimental results of Table 1 stored in arrays.
    var height = [15,16,17,18,19];
    var number = [4,5,11,10,6];
    
    //Part (ii).
    //Write code to declare and initialise new array to represent the third row of the table.
    var myarray = new Array(5)
    var myarray = ["60","80","187","180","114"] ;
    
    //Part  (iv).
    //Write code to calculate the average height and write it out in the browser window.
    var values = [60, 80, 187, 180, 114];
    var total = 0;
    for ( var i = 0; i < values.length; ++i )
    {
        total += values[i];
    }
    var myarray = total / values.length;
    document.write("The average height " + values.join(",") + " is " + myarray);
    
    </SCRIPT>
    
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: need help on a javascript please (newbie)

    When you are calculating the average on the line:
    Code:
    var myarray = total / values.length
    you are dividing the total value of your heights by the amount of values you have which is five. If you want to get the answer that you are looking for, you need to add up the values of the array titled total in a loop and then divide your total by the total of that array. The code to do this might look something like this:
    Code:
    //Insert between the for loop and the reasignment of myarray
    var averageDivisor = 0;
    
    for(var i = 0; i < number.length; i++)
         averageDivisor += numbers[i];
    
    var myarray = total / averageDivisor;
    //your output remains the same
    This should output the number you are looking for. The math was being done correctly all along, but you have to have the right input to get the right output . Hope this fixes your problem.

  4. #3
    kelly12345 is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Re: need help on a javascript please (newbie)

    hi thank you for your patience
    is this what you mean
    please ignore my ignorance.

    kelly

    xxx

    Code:
    <HEAD>
    <TITLE>
    averages
    </TITLE>
    
    <SCRIPT LANGUAGE = "JavaScript">
     
    
    //Experimental results of Table 1 stored in arrays.
    var height = [15,16,17,18,19];
    var number = [4,5,11,10,6];
    
    //Part (ii).
    //Write code to declare and initialise new array to represent the third row of the table.
    var myarray = new Array(5)
    var myarray = ["60","80","187","180","114"] ;
    
    //Part  (iv).
    //Write code to calculate the average height and write it out in the browser window.
    var values = [60, 80, 187, 180, 114];
    var total = 0;
    for ( var i = 0; i < values.length; ++i )
    {
        total += values[i];
    }
    var averageDivisor = 0;
    
    for(var i = 0; i < number.length; i++)
         averageDivisor += numbers[i];
    
    var myarray = total / averageDivisor;
    
    var myarray = total / values.length;
    document.write("The average height " + values.join(",") + " is " + myarray);
    
    </SCRIPT>
    
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

  5. #4
    whitey6993's Avatar
    whitey6993 is offline Programming Expert
    Join Date
    Dec 2008
    Posts
    435
    Rep Power
    15

    Re: need help on a javascript please (newbie)

    Yes, except that for the code to work, you should remove the line:
    Code:
    var myarray = total/values.length;
    this line is unnecessary and will provide the same incorrect answer you are already receiving. Once you remove that line, your code should give you the output you are looking for.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hi A Newbie Here
    By Offshoreally in forum Introductions
    Replies: 3
    Last Post: 09-29-2010, 06:09 AM
  2. Newbie here
    By Obscurist in forum Introductions
    Replies: 6
    Last Post: 08-07-2010, 08:01 AM
  3. NEWBIE
    By FrancoP in forum Introductions
    Replies: 10
    Last Post: 10-30-2008, 09:46 AM
  4. Obviously a newbie
    By indian rep in forum Introductions
    Replies: 15
    Last Post: 07-24-2008, 09:16 AM
  5. newbie
    By panic buy in forum Introductions
    Replies: 7
    Last Post: 05-15-2007, 10:53 PM

Tags for this Thread

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