Forum Moderators: open

Message Too Old, No Replies

javascript newbie really needs help plz

         

littlegirlkelly

2:39 pm on Feb 5, 2010 (gmt 0)

10+ Year Member



hi all

I am kelly and i am having a real hard time trying to do this script :(
my boyfriend thinks i should stick to the sink so i want to show him that i can do this anyone care to help me wipe the smug look off his face ? (an no he does not do computers).


Ok i need to find the average height using javascript.

here is the numbers i have to use.
15,16,17,18,19 height of object
4,5,11,10,6 number of objects

now 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

what i need to do is get it to work out the maths and show in a window that average height is 17.25
it just is not doing what i want it to do haha

anyone help please


<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];


//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"] ;


//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;

document.write("The average height " + values.join(",") + " is " + myarray);

</SCRIPT>

</HEAD>
<BODY>
</BODY>
</HTML>



thanks
kelly
#*$!

londrum

4:04 pm on Feb 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i would probably do it like this. put the heights in one array, and the objects in another. and then just do a loop which multiplies
heights[0]
with
objects[0]
...
heights[1]
with
objects[1]
...
heights[2]
with
objects[2]
etc... and at the end of the loop you'll have the running total. and then just divide that by the total value in the objects array -- which you can total up at the same time as you're going through the original loop. that way you only have one loop.

littlegirlkelly

7:21 pm on Feb 5, 2010 (gmt 0)

10+ Year Member



A huge thank you to londrum
your a star thanks for your help it is now working ;)

thanks
kelly