PDA

View Full Version : for loops


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>

ez4me2c3d
01-24-2003, 02:57 AM
not to put you down, but your code is very sloppy. I suggest you develope some good coding practices. like i said no offense but I just couldn't let you go without making you aware that your code needs a lot of work. and i don't mean this particular script. anyway this works.

<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, leave blank and press ok when finished</div>


<script>
var Fruit = new Array();
var Prices = new Array();
var Produce = "none";
var Price = 1;
var i = 0;

while (Produce) {
Produce = prompt("Please enter your favorite fruit?", "" );
if (Produce) {
Fruit[i] = Produce;
i++;
}
}

document.write('<div align="center">');
for (i = 0; i < Fruit.length; i++) {
document.write(Fruit[i] + '<br />');
}
</script>
</div>

<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>
while (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], 0));
if (Price) {
alert(Price);
Prices[i] = Price;
document.write ("<tr><td>" + Fruit[i] + "<\/td><td>" + Prices[i] + "<\/td><\/tr>");
}
Price = null;
}
document.write("<\/table><\/div>");
}

</script>


</td>
</tr>
</table>
</body>
</html>

Kevin
01-24-2003, 03:57 AM
I appreciate the hand.
If I read your code correctly you placed Price as null to terminate the for() loop. Is this correct? If so how do I catch the white space entries and the blank entries?

Also I wouldn't take offense on my coding style.
I'm a student.

But my coding practices in the alt.comp.lang.learn.c-c+ forum are complimented regularly. I've been working hard in this area for some time now.

So it would be very cool if you could give me some pointers on good coding practices for javascript.

Not sure how you could do this as your code is all right aligned when posted. But one idea does popup and that is if you could repost this file to me via email using white space instead of tabs so the code dosen't collapse. This so I can see what you consider to be good coding techniques.

I would very much like my code to be as pretty:thumbsup: as possible. I tend to be a stickler for readablility.

any possible insights that you can pass along would be appreciated. I intend to code as well here as I do in C.

insight is always appreciated

Thank You
Kevin Raleigh
krr@ix.netcom.com

ez4me2c3d
01-24-2003, 01:13 PM
every person will code differently and I fully understand that there's not just one right way to code a certain script/program/function/etc. you do show sign of coding experience, but I was mainly commenting on the "pretty" and readability factor. There are a few uneccessary items you popped in there but they are signs of a beginner. No fault of your own.

if I were to fix your code to how I would say is "good", then I would be rewritting the whole page, HTML and all. Then that would go back to what I said about how everyone codes differently. I didn't mean to sound harsh, you obviously know your way around codes, so within time i have no doubts you will do just fine.

beetle
01-24-2003, 05:34 PM
Here's another example to chew on. the currency method is just fluff, and not necessary to get the task done. The process I used has a couple benefits

1) Uses just one loop to collect all the data
2) By making use of two functions, use of global variables is avoided.<html>
<head>
<title>Fruit Prices</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">

function getData()
{
var data = new Array();
var finished = false;
var i = 0;

while( !finished )
{
data[i] = new Object();
data[i].fruit = prompt( "Please enter a fruit name", "" );
if ( data[i].fruit == null || data[i].fruit == '' )
finished = true;
else
data[i].price = prompt( "Please enter the price ($/Kg) for " + data[i++].fruit, "" );
}
showData( data );
}

function showData( fruitArray )
{
document.write( '<table cellspacing="0" cellpadding="5" border="1" align="center">' );
for ( var i = 0; ( fruit = fruitArray[i] ); i++ )
{
document.write ("<tr><td>" + fruit.fruit + "<\/td><td>" + fruit.price.currency( 0, 1, 1 ) + "<\/td><\/tr>");
}
document.write( '</table>' );
}

String.prototype.currency = function( grouping, dollar, round )
{
var point = this.indexOf( "." );
var anynum = parseFloat( this );
var result = ( round ) ? Math.round( anynum * 100 ) / 100 : Math.floor( anynum * 100 ) / 100;
result = result.toString(10);

if (point == -1)
result = parseInt(anynum) + ".00";
else
{
var decimals = result.substring( point + 1 );
if ( decimals.length == 1 ) result = result + "0";
}

if ( grouping )
{
point = result.indexOf( "." );
var dollars = result.substring( 0, point );
var cents = result.substring( point );
var commas = parseInt( dollars.length / 3 );
var temp = new Array();
for ( var i = commas; i >= 0; i-- )
{
if ( dollars.length > 3 )
{
temp[i] = dollars.substring( dollars.length-3 );
dollars = dollars.substring( 0, dollars.length-3 );
}
else
{
temp[i] = dollars;
}
}
dollars = temp.join( "," );
result = dollars + cents;
}
return ( dollar ) ? "$ " + result : result;
}


</script>

</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">Even more loops & Functions<br>
<br>
For each fruit enter the price in $/Kg.<br>
The Price of Fruit ($/Kg)</div>
<script type="text/javascript">
getData();
</script>
</td>
</tr>
</table>

</body>
</html>