Results 1 to 2 of 2

Thread: javascript code calculating average height

  1. #1
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy javascript code calculating average height

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


    Code:
    <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
    Last edited by jscheuer1; 03-03-2010 at 03:00 AM. Reason: format code

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

    Default

    Its because:
    Code:
    plantTotalNumbers[36] = plantNumbers[4,5,11,10,6] * plantHeights[60,80,187,180,114];
    Code:
    totalproducts = totalproducts + plantTotalNumbers[36];
    Code:
    totalPlants = totalPlants + plantNumbers[36];
    Jeremy | jfein.net

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
  •