Kevin
01-24-2003, 12:45 AM
I have a couple of functions. One gets the names of fruits and then loads it into an array. It then displays them to the page centered.
Next I prompt the user for prices. I use the length property of my fruit array to determine the number of times the user is prompted for prices. Once the end of the fruit array is reached the for loop() should terminate and print the results into a nice table. However for some reason it appears to start at the beginning of the array again.
Can you advise?
THE CODE
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="styleSheet.css" type="text/css">
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="background.jpg">
<table width="75%" border="0" cellpadding="0" cellspacing="0" align="left">
<tr>
<td><img src="header.jpg" width="764" height="72"></td>
</tr>
</table>
<br clear="all"/>
<table width="19%" border="0" cellpadding="0" cellspacing="0" align="left">
<tr>
<td><img src="nav.jpg" width="189" height="614"></td>
</tr>
</table>
<table width="573" border="0" cellspacing="0" cellpadding="10">
<tr align="left" valign="top">
<td>
<h1 align="center">Lab 2 - Part 2</h1>
<div align="center"><b>Data types, variables, Decisions, loops and functions.<br>More Loops</b></div>
<br>
<div align="center">Enter the names of fruits, Select Cancel when finished</div>
<script language="JavaScript1.2" type="text/javascript">
<!--
var ProduceMess = "Enter your favorite fruit and hit enter.\n When finished just hit the cancel button";
alert (ProduceMess);
var fruit = new Array();
var i = 0;
function FillArray()
{
var Produce = 1;
while (Produce != "" || Produce == " ")
{
Produce = prompt ("Please enter your favorite fruit?", "" );
if(!Produce)
break;
fruit[i] = Produce;
i++;
}
document.write("<div align='center'>");
for (i = 0; i < fruit.length; i++)
{
document.write (fruit[i] + "<br>");
}
document.write("<\/div>");
}
FillArray();
//-->
</script>
<div align="center">Even more loops & Functions<br>
<br>
For each fruit enter the price in $/Kg.<br>
The Price of Fruit ($/Kg)</div>
<script language="JavaScript1.2" type="text/javascript">
<!--
var Prices = new Array();
var j = 0;
function GetPrices ()
{
var Price = 1;
while (Price != "" || Price == " ")
{
document.write("<table cellspacing=0 cellpadding=5 border=1 align='center'>");
for (i=0; i < fruit.length; i++)
{
Price = parseFloat (prompt ( "Please enter the price of" + fruit[i], 11.99));
if (!Price)
break;
Prices[j]= Price;
document.write ("<tr><td>" + fruit[i] + "<\/td><td>" + Price[j] + "<\/td><\/tr>");
} /* for */
document.write("<\/table><\/div>");
} /* while */
} /* end GetPrice() */
//-->
</script>
<script language="JavaScript1.2" type="text/javascript">
<!--
GetPrices ();
//-->
</script>
</td>
</tr>
</table>
</body>
</html>
Next I prompt the user for prices. I use the length property of my fruit array to determine the number of times the user is prompted for prices. Once the end of the fruit array is reached the for loop() should terminate and print the results into a nice table. However for some reason it appears to start at the beginning of the array again.
Can you advise?
THE CODE
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="styleSheet.css" type="text/css">
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" background="background.jpg">
<table width="75%" border="0" cellpadding="0" cellspacing="0" align="left">
<tr>
<td><img src="header.jpg" width="764" height="72"></td>
</tr>
</table>
<br clear="all"/>
<table width="19%" border="0" cellpadding="0" cellspacing="0" align="left">
<tr>
<td><img src="nav.jpg" width="189" height="614"></td>
</tr>
</table>
<table width="573" border="0" cellspacing="0" cellpadding="10">
<tr align="left" valign="top">
<td>
<h1 align="center">Lab 2 - Part 2</h1>
<div align="center"><b>Data types, variables, Decisions, loops and functions.<br>More Loops</b></div>
<br>
<div align="center">Enter the names of fruits, Select Cancel when finished</div>
<script language="JavaScript1.2" type="text/javascript">
<!--
var ProduceMess = "Enter your favorite fruit and hit enter.\n When finished just hit the cancel button";
alert (ProduceMess);
var fruit = new Array();
var i = 0;
function FillArray()
{
var Produce = 1;
while (Produce != "" || Produce == " ")
{
Produce = prompt ("Please enter your favorite fruit?", "" );
if(!Produce)
break;
fruit[i] = Produce;
i++;
}
document.write("<div align='center'>");
for (i = 0; i < fruit.length; i++)
{
document.write (fruit[i] + "<br>");
}
document.write("<\/div>");
}
FillArray();
//-->
</script>
<div align="center">Even more loops & Functions<br>
<br>
For each fruit enter the price in $/Kg.<br>
The Price of Fruit ($/Kg)</div>
<script language="JavaScript1.2" type="text/javascript">
<!--
var Prices = new Array();
var j = 0;
function GetPrices ()
{
var Price = 1;
while (Price != "" || Price == " ")
{
document.write("<table cellspacing=0 cellpadding=5 border=1 align='center'>");
for (i=0; i < fruit.length; i++)
{
Price = parseFloat (prompt ( "Please enter the price of" + fruit[i], 11.99));
if (!Price)
break;
Prices[j]= Price;
document.write ("<tr><td>" + fruit[i] + "<\/td><td>" + Price[j] + "<\/td><\/tr>");
} /* for */
document.write("<\/table><\/div>");
} /* while */
} /* end GetPrice() */
//-->
</script>
<script language="JavaScript1.2" type="text/javascript">
<!--
GetPrices ();
//-->
</script>
</td>
</tr>
</table>
</body>
</html>