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);
"<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
Bookmarks