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

What am I doing wrong?

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
DE
I'm very, very new at javascript so please be patient with me. I'm trying to write a for loop that will generate links for me, depending on the month. When I try to run the script firebug keeps giving me this error:

missing ] in index expression
Month[11] = new Array(2)\n

Can anyone help me?
Here is the code I'm trying to run:

Code:
<script language="javascript">
			CurrentMonth = "April"
			CalSrc = "http://[DOMAIN...]/Calendars/BeaumontYSACalendar"
			Month = new Array(13)
			Month[0] = new Array(2)
			Month[0][0] = "January"
			Month[0][1] = "Jan09.html"
			Month[1] = new Array(2)
			Month[1][0] = "February"
			Month[1][1] = "Feb09.html"
			Month[2] = new Array(2)
			Month[2][0] = "March"
			Month[2][1] = "Mar09.html"
			Month[3] = new Array(2)
			Month[3][0] = "April"
			Month[3][1] = "Apr09.html"
			Month[4] = new Array(2)
			Month[4][0] = "May"
			Month[4][1] = "May09.html"
			Month[5] = new Array(2)
			Month[5][0] = "June"
			Month[5][1] = "Jun09.html"
			Month[6] = new Array(2)
			Month[6][0] = "July"
			Month[6][1] = "Jul09.html"
			Month[7] = new Array(2)
			Month[7][0] = "August"
			Month[7][1] = "Aug09.html"
			Month[8] = new Array(2)
			Month[8][0] = "September"
			Month[8][1] = "Sep09.html"
			Month[9] = new Array(2)
			Month[9][0] = "October"
			Month[9][1] = "Oct09.html"
			Month[10] = new Array(2)
			Month[10][0] = "November"
			Month[10[1] = "Nov09.html"
			Month[11] = new Array(2)
			Month[11][0] = "December"
			Month[11][1] = "Dec09.html"
			
			//Link Format:
			// | <a href=" [HTML SOURCE]Mmm-YY.html "> [MONTH NAME] </a> |
			// Where [HTML SOURCE] = Everything in the URL preceding the final extension of each months page URL.
			// Example Source: http://[DOMAIN...]/Calendars/BeaumontYSACalendarMar-09.html
			
			for(i=0;i<12;++i) {
				if (Month[i][0]==CurrentMonth) {
					// If the month variable matches the current month, highlight the text in red, but don't make a link
					document.write('<font color="red"><strong>' + Month[i][0] + '</strong></font> | ')
					if (i==5) {
						document.write("<br />")  // Insert a line break after June
					}
				} else {
					// If the month variable does not match the current month, produe a link to that month calendar
					document.write('<a href="' + CalSrc + Month[i][1] + '">' + Month[i][0] + '</a> | ')
					if (i==5) {
						document.write("<br />")  // Insert a line break after June
					}
				}
			}			
		//
</script>

-JTBorton
Another Day, Another Disaster
 
Small syntax error dude.

Change:

Code:
Month[10[1] = "Nov09.html"

to

Code:
Month[10][1] = "Nov09.html"
 
Another tip- you can shorten you code by doing this:

Code:
            Month = new Array(12)
            Month[0] = ["January", "Jan09.html"];
            Month[1] = ["February","Feb09.html"];
            Month[2] = ["March", "Mar09.html"];
            Month[3] = ["April", "Apr09.html"];
            Month[4] = ["May", "May09.html"];
            Month[5] = ["June", "Jun09.html"];
            Month[6] = ["July", "Jul09.html"];
            Month[7] = ["August", "Aug09.html"];
            Month[8] = ["September", "Sep09.html"];
            Month[9] = ["October","Oct09.html"];
            Month[10] = ["November", "Nov09.html"];
            Month[11] = ["December", "Dec09.html"];

This way you don't have to create so many array objects.
 
Hi

Then why not shorten it even more ?
Code:
Month = [
  ["January", "Jan09.html"],
  ["February","Feb09.html"],
  ["March", "Mar09.html"],
  ["April", "Apr09.html"],
  ["May", "May09.html"],
  ["June", "Jun09.html"],
  ["July", "Jul09.html"],
  ["August", "Aug09.html"],
  ["September", "Sep09.html"],
  ["October","Oct09.html"],
  ["November", "Nov09.html"],
  ["December", "Dec09.html"]
];
Anyway, I would shorten it even more by using an associated array :
Code:
CurrentMonth = "April"
CalSrc = "http://[DOMAIN...]/Calendars/BeaumontYSACalendar"
Month= {
  "January":"Jan09.html",
  "February":"Feb09.html",
  "March":"Mar09.html",
  "April":"Apr09.html",
  "May":"May09.html",
  "June":"Jun09.html",
  "July":"Jul09.html",
  "August":"Aug09.html",
  "September":"Sep09.html",
  "October":"Oct09.html",
  "November":"Nov09.html",
  "December":"Dec09.html"
}

i=0
for (m in Month) {
  if (m==CurrentMonth) {
     document.write('<font color="red"><strong>' + m + '</strong></font> | ')
  } else {
    document.write('<a href="' + CalSrc + Month[m] + '">' + m + '</a> | ')
  }
  if (i++==5) document.write("<br />")
}
But in fact, I see no reason to store the month names twice :
Code:
CurrentMonth = "April"
CurrentYear = "09"
CalSrc = "http://[DOMAIN...]/Calendars/BeaumontYSACalendar"
Month = [ "January",  "February",  "March",  "April",  "May",  "June",  "July",  "August",  "September",  "October",  "November",  "December" ]

i=0
for (m in Month) {
  if (Month[m]==CurrentMonth) {
     document.write('<font color="red"><strong>' + Month[m] + '</strong></font> | ')
  } else {
    document.write('<a href="' + CalSrc + Month[m].substr(0,3) + CurrentYear + '.html">' + Month[m] + '</a> | ')
  }
  if (i++==5) document.write("<br />")
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top