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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass an Array to a Function

Status
Not open for further replies.

unvme

IS-IT--Management
Jan 20, 2003
29
US
I apologize in advance, because I am kinda new to this whole Javascript thing, so I'll try to explain my situation as best as possible. I am not getting any errors, but the script seems to freeze when trying to run through this function:

Code:
//calculate individual Per Diem Days & populate fields
function perDiem(arr1, arr2) {
	for (var i=1; i<=8; i++) {
		var days = 0;
		var dec = days.toFixed(2);

		if (i == 1) {
			days = calcDepart(arr1[i - 1]);
		}
		if (document.getElementById('aDate' + (i - 1)).value != "") {
			days = days + calcBusNextDest(arr1[i]) + calcFullBus(arr1[i - 1], arr1[i]);
		} else if (document.getElementById('aDate' + i).value != "" && document.getElementById('dDate' + i).value != "") {
			days = days + calcBusDest(arr1[i]) + calcFullBus(arr1[i - 1], arr1[i]);
		} else if (document.getElementById('aDate' + i).value != "" && document.getElementById('dDate' + i).value == "") {
			days = days + calcReturn(arr2[i-1], arr1[i]);
		}

		//convert number value to a string
		dec = dec + "";
alert("perDiem days" + i + "=" + dec);
		//populate whole day fields
		document.getElementById("dayWhole" + i).value = dec.substring(0, dec.indexOf('.'));
		//populate quarter day fields
		document.getElementById("dayQuarter" + i).selectedIndex = getQuarter(dec);
	}
}

Here are the 2 arrays that I'm trying to pass into the above function:

Code:
//read and store aDate and aTime fields
function storeArrival(form) {
	arrival = new Array();
	
	for (var i=0; i<=8; i++) {
		var hour = 0;
		var minute = 0;

		var date = document.getElementById('aDate' + i).value;

		if (date != null) {
			arrival[i] = new Date();

			arrival[i].setMonth(date.substring(0, date.indexOf('/')) - 1);
			arrival[i].setDate(date.substring(date.indexOf('/') + 1, date.lastIndexOf('/')));
			arrival[i].setYear(date.substring(date.lastIndexOf('/') + 1, date.length) + 100);

			var time = document.getElementById('aTime' + i).value;
		
			hour = parseInt(time.substring(0, time.indexOf(":")), 10);
			minute = parseInt(time.substring(time.indexOf(":") + 1, time.length), 10);

			if (hour == 12 && document.getElementById('aAMPM' + i).selectedIndex == 0) {hour = hour - 12};
			if (hour != 12 && document.getElementById('aAMPM' + i).selectedIndex == 1) {hour = hour + 12};

			arrival[i].setHours(hour);
			arrival[i].setMinutes(minute);
			arrival[i].setSeconds(0);
			arrival[i].setMilliseconds(0);
		}
alert("arrival" + i + "=" + arrival[i]);
	}
	return arrival;
}

//read and store dDate and dTime fields
function storeDeparture() {
	depart = new Array();

	for (var i=0; i<=8; i++) {
		var hour = 0;
		var minute = 0;

		var date = document.getElementById('dDate' + i).value;

		if (date != null) {
			depart[i] = new Date();

			depart[i].setMonth(date.substring(0, date.indexOf('/')) - 1);
			depart[i].setDate(date.substring(date.indexOf('/') + 1, date.lastIndexOf('/')));
			depart[i].setYear(date.substring(date.lastIndexOf('/') + 1, date.length) + 100);

			var time = document.getElementById('dTime' + i).value;

			hour = parseInt(time.substring(0, time.indexOf(":")), 10);
			minute = parseInt(time.substring(time.indexOf(":") + 1, time.length), 10);

			if (hour == 12 && document.getElementById('dAMPM' + i).selectedIndex == 0) {hour = hour - 12};
			if (hour != 12 && document.getElementById('dAMPM' + i).selectedIndex == 1) {hour = hour + 12};

			depart[i].setHours(hour);
			depart[i].setMinutes(minute);
			depart[i].setSeconds(0);
			depart[i].setMilliseconds(0);
		}
alert("depart" + i + "=" + depart[i]);
	}
	return depart;
}

And here is the calcDepart() function that gets called in the PerDiem() function (this is where the script seems to get frozen). The other functions called in the PerDiem() function are similar to this calcDepart() function:

Code:
//calculate Per Diem Days allowed for departure from Home Base
function calcDepart(arr) {
	var total = 0;
alert("calcDepart");
	while (arr.getHours() >= 0 && arr.getHours() <= 6) {
		if (arr.getHours() == 0 && arr.getMinutes() < 1) {total = 0.25;}
		else if (arr.getHours() == 6 && arr.getMinutes() > 0) {total = 0.75;}
		else total = 1.00;
	}
	while (arr.getHours() >= 7 && arr.getHours() <= 12) {
		if (arr.getHours() == 12 && arr.getMinutes() > 0) {total = 0.50;}
		else total = 0.75;
	}
	while (arr.getHours() >= 13 && arr.getHours() <= 18) {
		if (arr.getHours() == 18 && arr.getMinutes() > 0) {total = 0.25;}
		else total = 0.50;
	}
	while (arr.getHours() >= 19 && arr.getHours() <= 23) {
		total = 0.25;
	}alert("total=" + total.toFixed(2))
	return total.toFixed(2);
}

The script seems to run fine if I don't enter any dates into the form's fields, but once I enter a date, it freezes up. I'm not sure what I'm doing wrong, but I'm sure I have a lot of syntactical errors. I'm thinking that I'm referencing the arrays incorrectly within the PerDiem() function. ANY help would be greatly appreciated.

Thanks!
 
start by putting some alert() calls in your functions to show variable values to see if it's behaving as you expect.

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Not sure what it could be, but I did find an error I made in the past.
In a always use the "var" when declaring a variable.
What happens when you don't then the variable becomes global.

var depart = new Array();
Like jemminger said you going to have to put alert() to stop an see.
When I run into this I remove everything in the function and return an empty array. Then start adding back piece by piece and you will discover where it hangs.

 
the script seems to freeze

That would probably point to one of your while statements never ending - check the logic on them, to make sure it is sound.

You might try using IE and the free Microsoft Script Debugger, or Firefox and the free Venkman Debugger to step through your code, and watch the variables to see what the issue is.

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
In fact, I'd say it probably is your while statement. Look at the very first one:

Code:
while (arr.getHours() >= 0 && arr.getHours() <= 6)

Given that the values returned by arr.getHours and arr.getMinutes are never being changed / set inside the loop:

Code:
{
   if (arr.getHours() == 0 && arr.getMinutes() < 1) {total = 0.25;}
   else if (arr.getHours() == 6 && arr.getMinutes() > 0) {total = 0.75;}
   else total = 1.00;
}

then if the loop was ever executed, it would go on for ever, clearly.

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 

Hmm.. the same is true with ALL your while statements. Did you mean to use while statements, or did you mean to use if statements?

If you meant to use while statements, you will have to change either the hour or minute values of arr, otherwise the exit condition will NEVER be met.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top