Problems with return

Hi,

Im making a sort of callendar, and I’ve ran upon some problems with a function.


tMonth = new Array();
tMonth = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
tMonthDays = new Array();
tMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

mConstruct = function(d, tabname) {
	for (i = 1; i < tMonthDays[d] + 1; i++) {
			tabname = new Array();
			tabname* = i;
		}
	return tabname;
};

asdf = mConstruct(0, february);
trace(asdf);

This is just a part of the script, the whole thing will kinda look like a calendar, it will load certain dates from an XML file, cross-check them with the dates, and then it will mark them in a different way in the SWF. However this sample of code should just build arrays of days using the name of the month array, and days in it array.

Later Im gonna check if dates from the xml match some dates in those arrays. If they will, it will change theyr apperance.

Tell me if it’s a good idea for such a thing. I don’t have much experience in coding, so I might be going the wrong way on this one.

Thx.

What is that tabname parameter for?

You have been declaring tabname every pass of that for loop. You should do it only once…

tMonth = new Array();
tMonth = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
tMonthDays = new Array();
tMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

mConstruct = function (d) {
	var tabname = new Array();
	for (i=0; i<tMonthDays[d]; i++) {
		tabname* = i+1;
	}
	return tabname;
};
asdf = mConstruct(0);
trace(asdf);

Actually the problem lies within the function declaration. It isnt returning a data type.[AS]mConstruct = function (d:Number):Array {
var tabname = new Array();
for (i=0; i<tMonthDays[d]; i++) {
tabname* = i+1;
}
return tabname;
};[/AS]try that


mConstruct = function (d:Number, tabname:Array) {
    var tabname = new Array();
    for (i=0; i<tMonthDays[d]; i++) {
        tabname* = i+1;
    }
    return tabname;
};

asdf = mConstruct(0, feb);
trace(asdf);

So this works, but does the array get written in the memory? Cause I can’t access it by a simple loop.


for (i=0; i < feb.length; i++) {
	trace(feb*);
}

You can use:

 [FONT=Courier New][LEFT]mConstruct = [COLOR=#000000]**function**[/COLOR] [COLOR=#000000]([/COLOR]d:[COLOR=#0000ff]Number[/COLOR], tabname:[COLOR=#0000ff]Array[/COLOR][COLOR=#000000])[/COLOR]:[COLOR=#0000ff]Array[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR]i=[COLOR=#000080]0[/COLOR]; i&lt;tMonthDays[COLOR=#000000][[/COLOR]d[COLOR=#000000]][/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
    tabname[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR] = i+[COLOR=#000080]1[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#0000ff]return[/COLOR] tabname;

[COLOR=#000000]}[/COLOR];

feb = [COLOR=#000000]new[/COLOR] [COLOR=#0000ff]Array[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#0000ff]trace[/COLOR][COLOR=#000000]([/COLOR]mConstruct[COLOR=#000000]([/COLOR][COLOR=#000080]0[/COLOR], feb[COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR];[/LEFT]
[/FONT]

or

 [FONT=Courier New][LEFT]mConstruct = [COLOR=#000000]**function**[/COLOR] [COLOR=#000000]([/COLOR]d:[COLOR=#0000ff]Number[/COLOR], tabname:[COLOR=#0000ff]String[/COLOR][COLOR=#000000])[/COLOR]:[COLOR=#0000ff]Array[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR]tabname[COLOR=#000000]][/COLOR] = [COLOR=#000000]**new**[/COLOR] [COLOR=#0000ff]Array[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR]i=[COLOR=#000080]0[/COLOR]; i&lt;tMonthDays[COLOR=#000000][[/COLOR]d[COLOR=#000000]][/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
    [COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR]tabname[COLOR=#000000]][/COLOR][COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR] = i+[COLOR=#000080]1[/COLOR];
[COLOR=#000000]}[/COLOR]
[COLOR=#0000ff]return[/COLOR] [COLOR=#0000ff]this[/COLOR][COLOR=#000000][[/COLOR]tabname[COLOR=#000000]][/COLOR];

[COLOR=#000000]}[/COLOR];

mConstruct[COLOR=#000000]([/COLOR][COLOR=#000080]0[/COLOR], [COLOR=#ff0000]“feb”[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR]i=[COLOR=#000080]0[/COLOR]; i < feb.[COLOR=#0000ff]length[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#0000ff]trace[/COLOR]COLOR=#000000[/COLOR];
[COLOR=#000000]}[/COLOR];
[/LEFT]
[/FONT]

Ok, it’s almost done. I just have one more bug to fix. Mainly when I add more than one date in the same month, only the last day gets marked in the swf.

http://www.wgtour.com/bohner/work/mikolajki/calendar.html
http://www.wgtour.com/bohner/work/mikolajki/calendar.zip - Source

XML:


<?xml version="1.0" encoding="iso-8859-1"?>
<reservations>
	<date day="1" month="1"></date>
	<date day="2" month="1"></date>
</reservations>

AS:


xmlLoad = new XML();
xmlLoad.ignoreWhite = true;
xmlLoad.load("dates.xml");
xmlLoad.onLoad = xmlRun;
tMonth = new Array();
tMonth = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
tMonthDays = new Array();
tMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

var n:Number = tMonth.length;
build = function(n:Number):Array {
	monthArray = new Array();
	for (i = 0; i < n; i++) {
		arrayName = tMonth*;
		arrayName = new Array();
		monthArray* = arrayName;
			for (j = 0; j < tMonthDays*; j++) {
				monthArray*[j] = j + 1;
			}
	}
	return monthArray;
};
build(n);


function xmlRun(success) {
	if (success) {
		xmlRoot = this.firstChild;
		xmlNodes = xmlRoot.childNodes;
		tDayReserv = new Array();
		tDayReserv*[j] = xmlRoot.childNodes*.attributes.day;
		for (i = 0; i < xmlNodes.length; i++) {
			m = xmlRoot.childNodes*.attributes.month - 1;
			d = xmlRoot.childNodes*.attributes.day - 1;
			tDayReserv[m] = new Array();
			tDayReserv[m][d] = d + 1;
		}
		for (i = 0; i < tMonth.length; i++) {
			_root["tM" + i].autoSize = true;
			_root["tM" + i].multiline = true;
			_root["tM" + i].wordWrap = true;
			_root["tM" + i].selectable = false;
			_root["tM" + i].html = true;
			_root["tM" + i].htmlText = tMonth*;
			for (j = 0; j < tMonthDays*; j++) {
				if (monthArray*[j] == tDayReserv*[j]) {
					_root["tM" + i].htmlText += "<B><FONT COLOR='#CC0000'>" + monthArray*[j] + "</FONT></I></B>";
				}
				_root["tM" + i].htmlText += monthArray*[j];
			}
		}
		//trace(tDayReserv[0][0] == monthArray[0][0]);
	}
	else {
		trace("XML not loaded");
	}
}

I think that it’s just a small thing that I’ve missed, but I can’t find it :confused: