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

calender style program, problem with for loop

Status
Not open for further replies.

Hobeau

MIS
Apr 19, 2004
77
US
Hey guys, I'm working on a calendar style program and I'm trying to set it up so that when I pick a date it will automatically display 3 days ahead of the day picked skipping saturdays and sundays. I'm still a newbie so I'm trying to do it using an array that is almost 3 weeks in length and the loop will simply go through the array and add 3 days excluding saturday and sunday. I havn't gotten to excluding saturday and sunday quite yet though as I'm struggling to just get the day to display correctly. I'm just trying to display the day in a textbox for now. Heres some of my code:
Code:
function sell(day){
var i = 0;
var index = 0;

week = new Array(21);
week[0] = "sunday";
week[1] = "monday";
week[2] = "teusday";
week[3] = "wednesday";
week[4] = "thursday";
week[5] = "friday";
week[6] = "saturday";
week[7] = "sunday";
week[8] = "monday";
week[9] = "teusday";
week[10] = "wednesday";
week[11] = "thursday";
week[12] = "friday";
week[13] = "saturday";
week[14] = "sunday";
week[15] = "monday";
week[16] = "teusday";
week[17] = "wednesday";
week[18] = "thursday";
week[19] = "friday";
week[20] = "saturday";

for (i=0; i<8; i++){
if (day.options[day.selectedIndex].value == week[i])
index = i;
}
document.getElementById("testdisplay").innerText = week[index + 3];	
}
I have I've done a few tests on my code so far and what I think is wrong is the if statement comparing the day object (a simple option list) with the "week" array. That seems to stop the code so that nothing after it works properly. Can anyone help?
 
How about this?
Code:
var week = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

function nextXWeekdays(start, x)
{
  while (x>0)
  {
    start++; // increment current position
    if (start == week.length) // if over
      start = 0; // return to beginning
    if (start != 0 && start != 6) // if not weekend
      x--; // move on to next one
  }
}

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Very nice. That'll work. I think one of my other main problems is the if statement. It doesnt seem to like comparing the selected option with the week array.
Code:
if (day.options[day.selectedIndex].value == week[i])
I thought that it was nothing more than comparing 2 strings, but I keep getting an error that is telling me that day.options[day.selectedIndex].value is returning a null value. ?!?! So I just displayed the value in a textbox and it worked fine. I'm kinda confused on this one.
 
oh and by the way,,, what is the "start" object being passed in? Is this just a variable?
 
Ok, I think I got most of it, but I'm still having problems with this if statement. I'm passing the options list (day) and for some reason I keep getting this error that the day.options[day.selectedIndex].value is returning a null value. This is my code now.

Code:
function sell(day){
var x = 3;

week = new Array("sunday", "monday", "teusday", "wednesday", "thursday", "friday", "saturday");

if (day.options[day.selectedIndex].value == "monday")
start=1;
else if (day.options[day.selectedIndex].value == "teusday")
start=2;
else if (day.options[day.selectedIndex].value == "wednesday")
start=3;
else if (day.options[day.selectedIndex].value == "thursday")
start=4;
else if (day.options[day.selectedIndex].value == "friday")
start=5;

while (x>0)
  {
    start++; // increment current position
    if (start == week.length) // if over
      start = 0; // return to beginning
    if (start != 0 && start != 6) // if not weekend
      x--; // move on to next one
  }	
document.getElementById("testdisplay").innerText= week[start];

}
 
ok,,, yeah,, I'm the biggest idiot ever,, for some strange reason I was trying to reference the <option> tag instead of the <select> tag,,,,, its working great,,, thanx for your help!
 
lol ... np.

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top