/    Sign up×
Community /Pin to ProfileBookmark

Novice Javascript user needs urgent help

Hi let me start by saying this is an examination question so am not after any answers just guidance.

I am having trouble with the following program. When I try to run it javascript fails to perform the calculation. Any ideas what I am doing wrong would be appreciated.

[code]
<HTML>
<HEAD>
<TITLE>
Plant Experiments
</TITLE>

<SCRIPT LANGUAGE = “JavaScript”>
/*
* M150 TMA03 2009J Q3.
* 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];
var totalProducts = 0
var totalPlants = 0

//Part (ii).
//Write code to declare and initialise new array to represent the third row of the table.
var product = 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.lengh; plant = plant + 1);
{
product[plant] = plantNumbers[plant] * plantHeights[plant];
}

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

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

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

</SCRIPT>

</HEAD>
<BODY>
</BODY>
</HTML>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Gozzy82Feb 25.2010 — you have some syntax errors:

for (var plant = 0; plant < plantNumbers.length; plant = plant + 1);

the for loop is different, you should not put a semicolon behind it

also you can use plant++ instead of plant = plant + 1

for (var plant = 0; plant < plantNumbers.length; plant++){}

if your not getting the desired output alert your variables before you do anything with them or log em to firebug so you know whats going on

like:

<i>
</i>alert(totalPlants);
alert(totalProducts);
document.write ('The average height of the plants in this experiement was ' + totalProducts / totalPlants + 'cm');


i
Copy linkTweet thisAlerts:
@Sterling_IsfineFeb 25.2010 — In addition to the ; errors, there is a typo in this line that in this rare case does not generate an error:
[CODE]for (var plant = 0; plant < plantNumbers.lengh; plant = plant + 1)[/CODE]
Copy linkTweet thisAlerts:
@KorFeb 25.2010 — Moreover:

1. Your document needs a Doctype

http://www.w3schools.com/tags/tag_DOCTYPE.asp

HTML strict would be enogh.

  • 2. [I]language[/I] is deprecated as a script attribute. Use the [B]type[/B] instead:
    <i>
    </i>&lt;script [COLOR="Blue"]type="text/javascript"[/COLOR]&gt;


  • 3. Don't use document.write() for this kind of job. It is not a dynamic method. Use DOM methods or [B]innerHTML[/B]
  • ×

    Success!

    Help @82brighteyes spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...