www.webdeveloper.com
Results 1 to 3 of 3

Thread: JavaScript Array stumper...

  1. #1
    Join Date
    Feb 2006
    Posts
    2

    Question JavaScript Array stumper...

    This is the guide lines for the program:




    Rainfall.htm (Transitional): Use a form. Write a program to read in rainfall amounts for some number of days from a single text box (use split to separate out the values). It then finds and displays the average rainfall and the number of days that had more than the average rainfall. The rainfall amounts and the average can be decimal numbers (e.g. .23 inches) so you will need to use parseFloat(). The values are stored in an array. Add the values and find the number of values there are. You will then calculate the average. After calculating the average go back through the array and find how many days it rained more than the average.

    You will need two separate loops. One to find the average rainfall and the other to find the number of days on which it rained more than average.

    CAUTION: You cannot assume that the values are always typed in with one blank between them and no leading or trailing blanks. You need to allow blanks everywhere (NaN test).



    I have written some code towards this project, however, I am getting some weird results. There is no syntax error, but I'm a noob when it comes to javaScripting. Can someone help me? I am bashing my head against the computer. Here is my code so far. (BTW I made my web page "Strict" and not "Transitional")



    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Rainfall</title>

    <script type="text/javascript">
    //<![CDATA[
    <!--
    document.writeln( "<form name = Rain action = none id=Rain>");
    document.writeln( "<p><a href=http://home.utah.edu/~u0418372>Home<\/a><\/p>");
    document.writeln( "Please enter the amount of rainfall in inches per day (ex. 18 10 2 12 ...)<br \/>");
    document.writeln( "<input name=rainfall type=text size=50 maxlength=75 \/>");
    document.writeln( "<input name=Continue type=button value=Continue onclick=ArrayFill() /><\/p>");
    document.writeln( "<\/form>");

    function ArrayFill() {
    var rainDays = new Array();
    var userInput = Rain.rainfall.value.split( " " );
    parseFloat(userInput);

    document.writeln( "<table border = \"1\" width =" + "\"100%\">" );
    document.writeln( "<thead><th width = \"100\"" + "align = \"left\">Days</th>" + "<th align = \"left\">Rain in inches</th></thead><tbody>" );

    for ( var j = 0; j < userInput.length; ++j ){
    rainDays [j]=j;
    document.writeln( "<tr><td>" + j + "</td><td>" + rainDays[ j ] + "</td></tr>" );
    document.writeln( "</tbody></table>" );
    }// close for statement

    }// end function ArrayFill()


    // -->
    //]]>
    </script>
    </head>
    <body>
    </body>
    </html>

  2. #2
    Join Date
    Feb 2006
    Posts
    2
    bump

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    made my web page "Strict" and not "Transitional"
    This case you should have code it strictly

    "<form name =\"Rain\" action =\"none\" id=\"Rain\">");

    and so on

    and: the document's elements have as root the document object
    var userInput = document.Rain.rainfall.value.split( " " );

    and: what is this?
    parseFloat(userInput);
    ??
    userInput is a collection, an array, so first you must circle through all the elements collection in order to do something with them. Furthermore, you have to assign that method to a variable, something like

    var something = parseFloat(userInput)[index]


    And, as long as you intend to use XML strict (by the way... Why on Earth you need an XML doctype?) then you should use DOM methods to create elements, not writeIn() method
    Last edited by Kor; 02-19-2006 at 12:49 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles