SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: array function

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array function

    I am trying to put together a function that does the following;
    if a value is not in the array ..put it in,
    else if a value is already in the array...take it out. Something along the lines of the code below (but it doesn't work). Has anyone seen such a function or can see where I am going wrong?
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script>
    
     var arr = new Array();
    
    function unique(el){
    for(var i=0;i<arr.length;i++){
    if(arr[i]=el){
    arr.splice(i,1);
    }
    else{
    arr.push(el);
    }
    }
    
       
    alert(arr);
    }
    </script>
    
    
    </script>
    </head>
    
    <body>
    <label>
    <input type="submit" name="button" id="button" value="Submit" onclick= "unique('1')"/>
    </label>
    <label>
    <input type="submit" name="button2" id="button2" value="Submit" onclick= "unique('2')"/>
    </label>
    <label>
    <input type="submit" name="button3" id="button3" value="Submit" onclick= "unique('3')" />
    </label>
    </body>
    </html>

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Classic mistake:
    Code:
    if(arr[i]=el){
    should use == or ===.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ooops! thanks.. but it doesn't make it work though.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What does the alert say? And what exactly are you trying to achieve?

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to be able to put values into an array and also be able to take them back out. I thought that I could do this by iterating through the array and if the value is found take it out..if not put it in. I have changed it a bit to read
    Code:
    <script>
    
     var arr = new Array();
    
    function unique(el){
    if(arr.length==0){
    arr.push(el);
    }
    else if(arr.length>0){
    
    for(var i=0;i<arr.length;i++){
    if(arr[i]==el){
    arr.splice(i,1);
    }
    else{
    arr.push(el);
    }
    
    }
    }
    
    alert(arr);
    }
    </script>
    The alert shows me that I can put one value in and take it out but the array doesn't build ie the array only hold one value ..I can't get an array of 1,2,3

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here, it's easier to show you the code rather than explain the problem:
    Code:
    var arr = [];
    function unique(el){
      if(arr.length==0){
        arr.push(el);
      }
      else if(arr.length>0){
        var found = false;
        for(var i=0;i<arr.length;i++){
          if(arr[i]==el){
            found = true;
            arr.splice(i,1);
          }
        }
        if (!found) {
          arr.push(el);
        }
      }
      alert(arr.toSource());
    }
    Basically the "else arr.push(el)" bit needed to be outside the for loop.

    It might be neater to use an object:
    Code:
    var stuff = {};
    function unique(el){
      if(el in stuff){
        delete stuff[el];
      }
      else {
        stuff[el] = true;
      }
      alert(stuff.toSource());
    }

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great..thanks for that! Works exactly how I want it to.


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
  •