Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textarea output help 1

Status
Not open for further replies.

k4ghg

Technical User
Joined
Dec 25, 2001
Messages
192
Location
US
I am new to Javascript and wrote a program that extrapolates two readings between several months. I want to send the data to forms textarea and am having some problems. I want add the month and a second value to my output (i.e., August "Calculated value1" "Calculated value2"). the code I am using is the follow:

function screenout(){
var test = 0;
for (i= mth1+1; i<= mth2; i++) {
test = test + fin+"\n";
document.displayform.itemsbox.value = test;

}
}

would some one please help me change this so I can added the additional information. Also, is there a way to limit the number of decimal places (i.e., 13.3434555 to 13.35). Thanks Ronnie
 
would some one please help me change this so I can added the additional information

Not without knowing where the additional information is stored, no. Unless we get very good at guessing, you'll have to give us [!]something[/!] to go on!

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Sorry - Here it the code. To test it I use Grass for the Crop, April first month, Aug last reading, first reading 12.09 and last reading 31.15. Thanks again:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="Generator" CONTENT="Winedit 2000">
<META NAME="Author" CONTENT="?">
<META NAME="Keywords" CONTENT="?">
<META NAME="Description" CONTENT="?">
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
oldvalue = "";
function passText() {

var crop = document.selectform.crop.value;
var frsmth = document.selectform.strMth.value;
var lstmth = document.selectform.endMth.value;
var oldread = document.selectform.freading.value;
var newread = document.selectform.lreading.value;

var Et = new Array(12);
var month = new Array(12);
var between = 0;
var mth1 = 0;
var mth2 = 0;
var deltap = 0;

var i = 0;

switch(frsmth){
case "jan":
mth1 = 0;
// alert (mth1);
break;
case "feb":
mth1 = 1;
break;
case "mar":
mth1 = 2;
break;
case "apr":
mth1 = 3;
break;
case "may":
mth1 = 4;
break;
case "june":
mth1 = 5;
break;
case "july":
mth1 = 6;
break;
case "aug":
mth1 = 7;
break;
case "sep":
mth1 = 8;
break;
case "oct":
mth1 = 9;
break;
case "nov":
mth1 = 10;
break;
case "dec":
mth1 = 11;
break;
default:
alert ("First month needs a value " + mth1);
}

switch(lstmth){
case "jan":
mth2 = 0;
break;
case "feb":
mth2 = 1;
break;
case "mar":
mth2 = 2;
break;
case "apr":
mth2 = 3;
break;
case "may":
mth2 = 4;
break;
case "june":
mth2 = 5;
break;
case "july":
mth2 = 6;
break;
case "aug":
mth2 = 7;
break;
case "sep":
mth2 = 8;
break;
case "oct":
mth2 = 9;
break;
case "nov":
mth2 = 10;
break;
case "dec":
mth2 = 11;
break;
default:
alert ("Last month needs a value "+lstmth +mth2);
}

between = (mth2 - (mth1-1));

if (between <= 0){
between = ((mth2-12) + mth1);
}

deltap = (newread + 1) - oldread;

switch(crop){
case "citrus":
return alert (crop);
case "grass":
var Et = new Array(12);
Et =[0.0472,0.0586,0.0782,0.0983,0.1216,0.0992,0.1123,0.1118,0.0899,0.0798,0.0584,0.0448];
calcgrass();
break;
case "fall":
return alert (crop);
case "spring":
return alert (crop);
case "strawberry":
return alert (crop);
case "nirr":
alert("nirr");
Et =[1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00];
calcgrass();
break;
return alert (crop);
default:
return alert (crop);
}

function calcgrass() {
var totEt = 0;
var testing = 0;
var i = 0;

for (i=(mth1+1) ; i <= mth2; i++){
totEt += Et;
}

var conv = new Array(12);
for (i=(mth1) ; i <= mth2+1; i++){
conv = (Et/totEt) * deltap;
}

var fin = new Array(12);
var newmeter = oldread;
for (i=(mth1+1) ; i <= mth2; i++){
fin = parseFloat(newmeter) + parseFloat(conv);
newmeter = fin;
}

screenout();

month= ["Jan =", "Feb = ", "Mar = ", "Apr = ", "May = ", "Jun = ", "Jul = ", "Aug = ", "Spt = ", "Oct = ", "Nov = ", "Dec = "];

function screenout(){
var test = 0;
for (i= mth1+1; i<= mth2; i++) {
// test = month + fin+"\n";
test = fin+"\n";
document.displayform.itemsbox.value += test;
}
}

}
}

// End -->
</script>

</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
<form name="selectform">
<font face="Arial, Helvetica, Sans Serif" size="3"><b>Select a Crop:</b></font><br>

<select name="crop" size=1>
<option value="grass">Grass</option>
<option value="citrus">Citrus</option>
<option value="fall">Fall Veg.</option>
<option value="spring">Spring Veg.</option>
<option value="strawberry">Strawberry</option>
<option value="nirr">No Irrigation</option>

</select>
<br><br>

<font face="Arial, Helvetica, Sans Serif" size="3"><b>Select Last Month With Reading:</b></font><br>
<select name="strMth" size=1>
<option value="jan">January</option>
<option value="feb">February</option>
<option value="mar">March</option>
<option value="apr">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="aug">August</option>
<option value="sep">September</option>
<option value="oct">October</option>
<option value="nov">November</option>
<option value="dec">December</option>
</select>
<br><br>

<font face="Arial, Helvetica, Sans Serif" size="3"><b>Select Restart Reading:</b></font><br>
<select name="endMth" size=1>
<option value="jan">January</option>
<option value="feb">February</option>
<option value="mar">March</option>
<option value="apr">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="aug">August</option>
<option value="sep">September</option>
<option value="oct">October</option>
<option value="nov">November</option>
<option value="dec">December</option>
</select>
<br><br>


<font face="Arial, Helvetica, Sans Serif" size="3"><b>&nbsp&nbsp&nbspOld Reading&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspNew Reading</b></font><br>
<input type="text" name="freading" maxlength = 15 size "18">
<input type="text" name="lreading" maxlength = 15 size "18">
<br><br>
<input type=button value="Calculate" onClick="passText();">
</form>

<form name="displayform" >
<font face="Arial, Helvetica, Sans Serif" size="3"><b>Results:</b></font><br>
<textarea cols="40" rows="13" name="itemsbox" ></textarea>
</form>


</BODY>

</HTML>
 
There's so much I'd change with your code... the DOCTYPE is incomplete, the character entities broken (you should have noticed this - they're all over the screen when you run the code), you are using font elements instead of CSS, and your HTML is generally malformed in many places (look at the size attributes of your input elements - no equals sign whatsoever). You are also using shortcut method to access the contents of the select elements - which doesn't work in all browsers, AFAIK.

Don't get me wrong - I'm not having a go at you - I know you've said you're new to this... I'm just pointing out some of the things you need to look at where you can improve on.

I'd also use slightly more meaningful and consistent variable names - why have "frsmth" & "strMth", and "lstmth" & "endMth"? Why not just "firstMonth" and "lastMonth" - it's so much easier to immediately see what they hold.

You should also be aware that array indexes in JavaScript start at 0, and I think it's possible some of your array access routines don't handle this very well... although I could be wrong, simply because I don't know what your code is designed to do (you've not told us).

I've converted your above code to be a lot neater, and more compact, although haven't sanity checked it, or made sure it works as intended (again, this is because you've not given us any details on how it is meant to work)... But hopefully you can use it as a better base to build upon.

As for adding a second value to your output... well - you haven't told us what value, so we still can't help you on that one. Psychic JavaScript developers are normally snapped up pretty quickly, you know.

Hope this helps,
Dan

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
	<meta http-equiv="content-language" content="en">
	<title>Test page</title>

	<script type="text/javascript">
	<!--

		function passText() {

			function calcgrass() {

				function screenout() {
					var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
					var test = '';
					for (var i=firstMonthIndex+1; i<=lastMonthIndex; i++){
						test += months[i] + ' = ' + fin[i]+ '\n';
					}
					document.forms['displayform'].elements['itemsbox'].value =  test;
				}

				var totEt = testing = 0;
				var conv = new Array(12);
				var fin = new Array(12);
				var newmeter = oldread;

				for (var i=firstMonthIndex; i<=lastMonthIndex; i++) totEt += Et[i];
				for (var i=firstMonthIndex; i<=lastMonthIndex+1; i++) conv[i] = (Et[i] / totEt) * deltap;
				for (var i=firstMonthIndex+1; i<= lastMonthIndex; i++) {
					fin[i] = parseFloat(newmeter) + parseFloat(conv[i]);
					newmeter = fin[i];
				}
				screenout();
			}

			var frm = document.forms['selectform'].elements;
			var crop = frm['crop'].options[frm['crop'].selectedIndex].value;
			var firstMonthIndex = frm['startMonth'].selectedIndex;
			var lastMonthIndex = frm['endMonth'].selectedIndex;
			var firstMonth = frm['startMonth'].options[firstMonthIndex].value;
			var lastMonth = frm['endMonth'].options[lastMonthIndex].value;
			var oldread = frm['freading'].value;
			var newread = frm['lreading'].value;

			var Et = new Array(12);
			var between = Math.abs(lastMonthIndex - firstMonthIndex - 1);
			var deltap = (newread + 1) - oldread;

			switch(crop) {
				case 'citrus':
					alert(crop);
					return;

				case 'grass':
					var Et = [0.0472, 0.0586, 0.0782, 0.0983, 0.1216, 0.0992, 0.1123, 0.1118, 0.0899, 0.0798, 0.0584, 0.0448];
					calcgrass();
					break;

				case 'fall':
					alert(crop);
					return;

				case 'spring':
					alert(crop);
					return;

				case 'strawberry':
					alert(crop);
					return;

				case 'nirr':
					alert(crop);
					Et = [1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00];
					calcgrass();
					break;

				default:
					alert(crop);
					return;
			}
		}

	//-->
	</script>

	<style type="text/css">
		body {
			background-color: #FFFFFF;
			color: #000000;
			font-family: Arial, Helvetica, sans-serif;
		}

		a {
			color: #FF0000;
		}
		a:visited {
			color: #800000;
		}
		a:focus, a:active {
			color: #FF00FF;
		}
	</style>
</head>

<body>
	<form name="selectform">
		Select a Crop:
		<select name="crop">
			<option value="grass">Grass</option>
			<option value="citrus">Citrus</option>
			<option value="fall">Fall Veg.</option>
			<option value="spring">Spring Veg.</option>
			<option value="strawberry">Strawberry</option>
			<option value="nirr">No Irrigation</option>
		</select>

		<br><br>
		Select Last Month With Reading:
		<select name="startMonth" size=1>
			<option value="jan">January</option>
			<option value="feb">February</option>
			<option value="mar">March</option>
			<option value="apr">April</option>
			<option value="may">May</option>
			<option value="june">June</option>
			<option value="july">July</option>
			<option value="aug">August</option>
			<option value="sep">September</option>
			<option value="oct">October</option>
			<option value="nov">November</option>
			<option value="dec">December</option>
		</select>

		 <br><br>
		Select Restart Reading:
		<select name="endMonth" size=1>
			<option value="jan">January</option>
			<option value="feb">February</option>
			<option value="mar">March</option>
			<option value="apr">April</option>
			<option value="may">May</option>
			<option value="june">June</option>
			<option value="july">July</option>
			<option value="aug">August</option>
			<option value="sep">September</option>
			<option value="oct">October</option>
			<option value="nov">November</option>
			<option value="dec">December</option>
		</select>

		<br><br>
		Old Reading
		<input type="text" name="freading" maxlength="15" size="18">

		<br><br>
		New Reading
		<input type="text" name="lreading" maxlength="15" size="18">

		<br><br>
		<input type="button" value="Calculate" onclick="passText();">
	</form>

	<form name="displayform">
		Results:
		<br>
		<textarea cols="40" rows="13" name="itemsbox"></textarea>
	</form>
</body>
</html>

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top