passing array to new fuction

W

windandwaves

Hi Folk

I have the following function
var a = new Array(4);
a[0] = new Array(45); //status (on or off)
a[1] = new Array(45); //name of the region
a[2] = new Array(45); //on mouse over
a[3] = new Array(45); //on mouse out

a[0][0] = false;
.....snip
a[1][0] = "region name";
......snip
a[2][1] = new Array('x', 'b','c','d',); // outer islands
a[2][2] = new Array('h', 'x', 'x', 'n');
....snip
a[3][1] = new Array('a', 'b','c','d',); // outer islands
a[3][2] = new Array('h', 'h', 'n', 'n');
....snip

function Rx(i) {
var j;
var dd = new Array();
document.getElementById('r'+i).checked ? j = 3 : j = 2;
dd = a[j];
si(dd);
}

function si() {
if (document.images && (preloadFlag == true)) {
var img;
for (var i=0; i<si.arguments.length; i+=2) {
img = null;
if (document.layers) {
img = findElement(si.arguments,0);
}
else {
img = document.images[si.arguments];
}
if (img) {
img.src = si.arguments[i+1]+'.gif';
}
}
}
}

My problem is when I call the Rx(i) function, it passes the whole array (e.g. a[2][3]) as one variable to si and not an array,
therefore, si does not work. What am I doing wrong?

There are about 200 a[j] variables, some of which are arrays in themselves.

TIA

- Nicolaas
 
R

RobG

windandwaves said:
Hi Folk [...]
...snip
a[3][1] = new Array('a', 'b','c','d',); // outer islands

You have a syntax error here -------^
a[3][2] = new Array('h', 'h', 'n', 'n');
...snip

function Rx(i) {
var j;
var dd = new Array();
document.getElementById('r'+i).checked ? j = 3 : j = 2;
dd = a[j];
si(dd);


Insert:

alert(dd);

to see that dd is an array.
}

function si() {
if (document.images && (preloadFlag == true)) {
var img;
for (var i=0; i<si.arguments.length; i+=2) {
img = null;
if (document.layers) {
img = findElement(si.arguments,0);
}
else {
img = document.images[si.arguments];


Here you are grabbing the arguments, but there is only one - the
entire array of what was formerly dd. Try:

...si.arguments[0]];

[...]

No warranty or guarantees provided.
 
R

RobB

RobG said:
windandwaves said:
Hi Folk [...]
...snip
a[3][1] = new Array('a', 'b','c','d',); // outer islands

You have a syntax error here -------^
a[3][2] = new Array('h', 'h', 'n', 'n');
...snip

function Rx(i) {
var j;
var dd = new Array();
document.getElementById('r'+i).checked ? j = 3 : j = 2;
dd = a[j];
si(dd);


Insert:

alert(dd);

to see that dd is an array.
}

function si() {
if (document.images && (preloadFlag == true)) {
var img;
for (var i=0; i<si.arguments.length; i+=2) {
img = null;
if (document.layers) {
img = findElement(si.arguments,0);
}
else {
img = document.images[si.arguments];


Here you are grabbing the arguments, but there is only one - the
entire array of what was formerly dd. Try:

...si.arguments[0]];

[...]

No warranty or guarantees provided.


Two syntax errors.
Not much point to this:

function Rx(i) {
var j;
var dd = new Array();
document.getElementById('r'+i).checked ? j = 3 : j = 2;
dd = a[j];
si(dd);
}

You've created a new array, assigned it to dd, and then overwritten
that with whatever was in the element a[j]. Why not just pass the
original reference?

si(a[j]);

As RobG hinted, you can't replace the arguments array (btw
'Function.arguments' was deprecated in favor of simply 'arguments'
around 1998) with another array passed in there; it'll be treated as
one argument.

function Rx(i) {
var el;
if (el = document.getElementById('r' + i))
si(a[el.checked ? 3 : 2]);
}

function si(arr) {
if (document.images
&& preloadFlag
&& typeof arr[0] != 'undefined') {
var img;
for (var i=0; i<arr.length; i+=2) {
img = null;
if (document.layers) {
img = findElement(arr,0);
}
else {
img = document.images[arr];
}
if (img) {
img.src = arr[i+1]+'.gif';
}
}
}
}

This sort of script is often made dramatically clearer (and more
efficient) by replacing the nested arrays with some sort of generic
object structure using named properties.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top