/    Sign up×
Community /Pin to ProfileBookmark

Pass Array to Function

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 also apologize for the abundance of code, but I’m not quite sure where the problem is. 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);
}
}
[/code]

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;
}
[/code]

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);
}
[/code]

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!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 18.2005 — You need to step it through a JavaScript Debugger so that you can check the results line by line.

http://www.webdeveloper.com/forum/showthread.php?t=69560
×

Success!

Help @unvme spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...