n-Dimensional Arrays
Sunday, 15. April 2007, 22:37:28
function nD(){
var A,tempA; //final and temp array
var dimension=arguments.length, i; //iteration vars
while(dimension--){
tempA = Array(arguments[dimension]);
i = tempA.length;
while(i--)
tempA[i]=A;
A = tempA;
}
return A;
};
function check(A){
return (""+A).split(",").length;
};
var test = nD(3,3,3); //create 3x3x3 'cube'
alert(test);
alert(check(test))); //check 'volume'
Or the short version:
l='length';
function nD(){
var a,b,c=arguments,d=c[l],i;
while(d--){
i = (b = Array(c[d]))[l];
while(i--)
b[i]=a;
a = b;
}
return a;
};
function check(a){
return (""+a).split(",")[l];
};
alert(nD(3,3,3));
alert(check(nD(3,3,3)));









