By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,750 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,750 IT Pros & Developers. It's quick & easy.

passing array to new fuction

P: n/a
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][i];
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[i],0);
}
else {
img = document.images[si.arguments[i]];
}
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[i][j] variables, some of which are arrays in themselves.

TIA

- Nicolaas

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
windandwaves wrote:
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][i];
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[i],0);
}
else {
img = document.images[si.arguments[i]];


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

...si.arguments[0][i]];

[...]

No warranty or guarantees provided.

--
Rob
Jul 23 '05 #2

P: n/a
RobG wrote:
windandwaves wrote:
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][i];
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[i],0);
}
else {
img = document.images[si.arguments[i]];


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

...si.arguments[0][i]];

[...]

No warranty or guarantees provided.

--
Rob


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][i];
si(dd);
}

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

si(a[j][i]);

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][i]);
}

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[i],0);
}
else {
img = document.images[arr[i]];
}
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.

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.