PDA

View Full Version : javascript code calculating average height



mamita
03-02-2010, 06:41 PM
Hey everyone,

just needed a quick help im trying to debug this code but i cant seem to find the prob in it..i wanna get an average of 17.25 but all am getting is NAN..



<HTML>
<HEAD>
<TITLE>
Plant Experiments
</TITLE>

<SCRIPT LANGUAGE = "JavaScript">
/*
* M150 TMA03 2009J Q3.
* Program to calculate average height of plants..
*
*/

//Experimental results of Table 1 stored in arrays.
var plantHeights = [15,16,17,18,19];
var plantNumbers = [4,5,11,10,6];
var totalproducts = 621
var totalPlants = 36

//Part (ii).
//Write code to declare and initialise new array to represent the third row of the table.
var plantTotalNumbers = new Array (5);

//Part (iv).
//Write code to calculate the average height of the plants and write it out in the browser window.

for (var plant = 0; plant < plantNumbers.length; plant = plant + 1);
{
plantTotalNumbers[36] = plantNumbers[4,5,11,10,6] * plantHeights[60,80,187,180,114];
}

for (var plant = 0; plant < plantNumbers.length; plant++)
{
totalproducts = totalproducts + plantTotalNumbers[36];
}

for (var plant = 0; plant < plantNumbers.length; plant = plant + 1);
{
totalPlants = totalPlants + plantNumbers[36];
}

document.write ('The average height of the plants in this experiement was ' + totalproducts / totalPlants + 'cm');


</SCRIPT>

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




Thanks in advance you guys...

gracie

Nile
03-02-2010, 11:53 PM
Its because:


plantTotalNumbers[36] = plantNumbers[4,5,11,10,6] * plantHeights[60,80,187,180,114];



totalproducts = totalproducts + plantTotalNumbers[36];
totalPlants = totalPlants + plantNumbers[36];