Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •