SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict adam2003w's Avatar
    Join Date
    Mar 2004
    Location
    colorado
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Weird Javascript output error: function (needle) ...

    How am I getting this extra output from my javascript array?

    Here's my code:
    Code JavaScript:
    <script language="Javascript" type="text/javascript">
     
    var myfiles=new Array()
    myfiles[0]="ASJ70001.jpg"; 
    myfiles[1]="ASJ70002.jpg"; 
    myfiles[2]="BA4100.jpg"; 
    myfiles[3]="BA4304A.jpg"; 
    myfiles[4]="BROK_UPS_CUSTOM.pdf"; 
    myfiles[5]="CA3105.jpg"; 
    myfiles[6]="GS40120.jpg"; 
    myfiles[7]="SSA25377.jpg"; 
    myfiles[8]="bitterbrush_77.jpg"; 
    myfiles[9]="p_logo01.gif"; 
    myfiles[10]="phpmailer_1.73.zip"; 
     
    for (x in myfiles) {
    	document.write(myfiles[x] + "<br />")
    }
     
    </script>
    What am i doing wrong to get this output?
    Code:
    ASJ70001.jpg
    ASJ70002.jpg
    BA4100.jpg
    BA4304A.jpg
    BROK_UPS_CUSTOM.pdf
    CA3105.jpg
    GS40120.jpg
    SSA25377.jpg
    bitterbrush_77.jpg
    p_logo01.gif
    phpmailer_1.73.zip
    function (needle) { var haystack = this; for (var i = 0; i < haystack.length; i++) { if (needle == haystack[i]) { return true; } } return false; }
    function (a) { for (var i = 0; i < a.length; i++) { this.push(a[i]); } return this; }

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    for (var i=0; i < myfiles.length; i++) {
    document.write(myfiles[i] + "<br />")
    }

  3. #3
    SitePoint Addict adam2003w's Avatar
    Join Date
    Mar 2004
    Location
    colorado
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome. Thanks for the quick response.

    I wonder why the other code didn't work. I got if from w3schools.com ...

    http://www.w3schools.com/js/tryit.as...s_array_for_in

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for...in is used to iterate through all members of an object. When used on an array, it not only returns the elements of that array but also the properties and methods of the array object (plus any new ones that have been assigned, like your needle/haystack function)


Bookmarks

Posting Permissions

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