PDA

View Full Version : return an array


ScottInTexas
02-09-2003, 05:06 PM
I have a routine that creates an array and I want the array returned to the calling function.


function setArray(){

var montharray=new Array();
var thisdate=new Date;
var thismonth=thisdate.getMonth();
for (var i=0;i<12;i++){
montharray[i]=thismonth;
thismonth++;
if (thismonth==12){
thismonth=0;
}
}
return montharray;
}

function CallingFunction(){

var thisArray=new Array();
thisArray=setArray();
}



This generates an error "Cannot assign to a function result".

Can you set the syntax straight?

Adam20002
02-09-2003, 06:41 PM
hi,

this probably won't help much but your code worked on my machine using IE6.

Adam

whammy
02-09-2003, 11:50 PM
var thisdate=new Date;

should be

var thisdate=new Date();

I don't know if that's the problem, though. This whole bit looks wrong to me:

for (var i=0;i<12;i++){
montharray[i]=thismonth;
thismonth++;
if (thismonth==12){
thismonth=0;
}
}


What are you trying to do? It looks like you're trying to define an array of months... but months are already defined in an array from 0-11 ?!?

beetle
02-10-2003, 05:39 AM
If I follow right, you want an array that has 0-11 but the 0 index containing the current month, say for June:

[5,6,7,8,9,10,11,0,1,2,3,4]

If that's the case, this should work...function setArray()
{
var months = [0,1,2,3,4,5,6,7,8,9,10,11];
var d = new Date();
var months2 = months.splice( 0, d.getMonth() );
return months.concat( months2 );
}