Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 12:40 AM   PM User | #1
ryan1234
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ryan1234 is an unknown quantity at this point
Array in function

Basically I have a global array which I use in a function and in this function values are assigned to the array depending on what the user enters.

I then need to use the array again with the assigned values, in another function. However I don't believe the assigned values will be in the array because of variable scope.

Is this correct, and if so how do I keep the values within the array after the function ends?

Thanks.
ryan1234 is offline   Reply With Quote
Old 10-10-2012, 12:43 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
if the array is in the global scope it should be available to all functions, and if the array has been changed within one function those changes are to the array itself - scope has nothing to do with it.

have the changes actually occurred, or does it just appear so?

it might be useful to see a sample of your code
xelawho is offline   Reply With Quote
Old 10-10-2012, 05:52 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,229
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Even if the array is *NOT* global, changes made in a function *WILL* affect its value in the caller. That's because in JavaScript *ALL* objects are passed by reference, and an array *IS* an object.

Here's a clear demo of that:
Code:
<script type="text/javascript">
function one( ary1 )
{
    ary1[0] = "banana pudding";
    ary1[3] = "zamboni";
}
function two( ary2 )
{
    alert( ary2.join("\n") );
}
function test() 
{
    var ary = [ 1, 2, 3 ];
    one( ary );
    two( ary );
}
test( );
</script>
As you can see, the array ary is *NOT* a global. Yet when we pass it to function one( ) it's first element is changed, as is proven when we then pass the same variable to function two( ).

And, as you can see, we can even add elements to the array and the added elements are carried back to the caller and then to the second callee.

THERE IS ONLY ONE ARRAY in all that code. The names ary, ary1 and ary2 are all actually referencing the same array.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 10-10-2012, 11:04 PM   PM User | #4
ryan1234
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ryan1234 is an unknown quantity at this point
The couple of functions I have are:

Code:
function save() {

//for (var i = 0; i <= 8; i++) {
//    myArray[i] = new Array(9);
//    for (var j = 0; j <= 8; j++) {
  //      myArray[i][j] = '';
    //}
//}

var col, row;
	for (row = 0; row <=8; row++) {
		for (col=0; col <= 8; col++) {
			var cell = document.getElementById('cell_' + col + '_' + row);
			if (parseInt(cell.innerHTML)) {
				myArray[[col][row]] = cell.innerHTML;				
			}
		}
	}				
}

function restore() {
var col, row;
	for (row = 0; row <=8; row++) {
		for (col=0; col <= 8; col++) {
			var cell = document.getElementById('cell_' + col + '_' + row);
			if (parseInt(cell.innerHTML)) {
				document.getElementById('cell_' + col + '_' + row).innerHTML = myArray[[col][row]];			
			}
			else {
				document.getElementById('cell_' + col + '_' + row).innerHTML = '';
				}
		}
	}
}
The array is declared earlier in the code.

Basically I use the save() function to save values from a table into the array. However when I don't enter a value into a particular cell (so that bit of array is empty), save it and press restore, it then puts a 6 into the cell.

Any ideas.
ryan1234 is offline   Reply With Quote
Old 10-11-2012, 12:20 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,229
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
This code is nonsense:
Code:
    myArray[[col][row]] = cell.innerHTML;
SURELY you actually mean
Code:
    myArray[col][row] = cell.innerHTML;
But I also don't understand this:
Code:
    if (parseInt(cell.innerHTML)) {
What are you expecting that to do??? Are you trying to find out if the cell contents are a number? Okay, that works. But better would be to use ! isNaN( )

In any case, though, you don't provide any default value when the if test fails, so you that array element WILL NOT BE DEFINED. Unless, of course, it was defined earlier.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:13 AM.


Advertisement
Log in to turn off these ads.