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 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question about Arrays

    Do arrays retain its data outside of a function. The reason why i asked this is because i created a global array and then a function. In the function i assaigned values to the global array but when i try to access the global array it gives me a "undefined" or "null" error.

    Code:
    var myArray = new Array();
    var count = 0; 
        
    function populate(){
         for(i=0; i < 5; i++){
             myArray[i] = count++;
    }
    
    }

  • #2
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Hi phenom01

    Yes, myArray would hold the values.

    Can we see the full code? (the code you are using to access it) As I suspect that is where the problem lies

    P.S. I should also probably mention that I think its technically preferrable to code it like so instead:

    Code:
    function populate(){
         for(i=0; i < 5; i++){
             myArray[i] = count;
             count++;
          }
    
    }
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Code:
    function test(){
         for(i=0; i < 5; i++){
              document.write(myArray[i]);
         }
    }

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Why do you need a separate count variable at all?
    Code:
    var myArray = [];
    
    function populate(){
         for(i=0; i < 5; i++){
             myArray[i] = i;
          }
    }
    But if you really only have 5 elements and they really are as simple as that, why not do
    Code:
    var myArray = [];
    
    function populate(){
         myArray = [0,1,2,3,4];
    }
    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.

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    because i am trying to see if arrays retain their values outside of a function. They are not retaining values.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    So put it all together. Since you shouldn't be using document.write() to create HTML outside of the <body>, and yet the document.write() must be done before the page is finished loading, you might as well just do it all in the <body>:

    Code:
    <html>
    <body>
    <script type="text/javascript">
    var myArray = [];
    
    function populate(){
         for(i=0; i < 5; i++){
             myArray[i] = i;
          }
    }
    function test(){
         for(i=0; i < 5; i++){
              document.write(myArray[i]);
         }
    }
    
    populate();
    test();
    </script>
    </body>
    </html>
    But of course that would also work like this:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var myArray = [];
    
    function populate(){
         for(i=0; i < 5; i++){
             myArray[i] = i;
          }
    }
    populate(); // must populate the array *before* page is loaded...
    
    // since this function does document.write and that MUST be done before end of page loading:
    function test(){
         for(i=0; i < 5; i++){
              document.write(myArray[i]);
         }
    }
    </script>
    </head>
    <body>
    Here is my test: 
    <script type="text/javascript">
    test();
    </script>
    </body>
    </html>
    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.

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    can't i do this on an external js file? What im really trying to do is load an xml file but IE gives me a "access denied" error. It works on firefox though but with firefox i cant use activeXobjet to write to files.

    Here is my code to load xml file into array:
    Code:
    function ReadXML() {
        xmlDoc = loadMyXML("xmlStudent.xml");
        alert(xmlDoc.childNodes.length);
    
        for (x = 0; x < xmlDoc.childNodes.length; x++) {
        var tmpPerson = new Array(xmlDoc.getElementsByTagName("studid")[x].childNodes[0].nodeValue,
        xmlDoc.getElementsByTagName("FN")[x].childNodes[0].nodeValue,
        xmlDoc.getElementsByTagName("LN")[x].childNodes[0].nodeValue);
        
        document.write(xmlDoc.getElementsByTagName("studid")[x].childNodes[0].nodeValue + ", " + xmlDoc.getElementsByTagName("FN")[x].childNodes[0].nodeValue + ", " + xmlDoc.getElementsByTagName("LN")[x].childNodes[0].nodeValue + "<br />");
        }

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Let's go move to your other thread, where you are trying to read the XML. That's where the real problem lies.
    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
    •