PDA

View Full Version : Resolved javascript help please.need to work out average



smellykelly
01-27-2010, 09:57 PM
Hi

Iam new here and new to javescript :confused:

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


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




<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

Nile
01-28-2010, 12:53 AM
To find average use this function:


Array.prototype.average = function(){
var avg = 0;
var old_this = this.length;
while(this.length > 0){
avg += this.shift();
}
return avg / old_this;
};

Usage:


var a = [1, 3];
return a.average();

smellykelly
01-29-2010, 12:21 PM
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 :D can shed some light on it i would be most greatful.

thanks in advance
kelly (some what dumbstruck)

Nile
01-29-2010, 12:59 PM
So I'm doing you're homework? :p


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

smellykelly
01-29-2010, 02:37 PM
So I'm doing you're homework? :p


<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 :rolleyes: 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 :mad:

Thanks for your help
kelly

Nile
01-29-2010, 08:24 PM
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"

gaz003
02-05-2010, 11:23 PM
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??

stumpped
02-17-2010, 10:29 PM
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