Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3601334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:36:33+00:00 2026-05-18T20:36:33+00:00

I was fed up with the limited javascript Array functions and wanted to write

  • 0

I was fed up with the limited javascript Array functions and wanted to write a few of my own handy prototype functions to perform Set Theory functions.

Below is the code I have for this so far

<script type="text/javascript">

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

Array.prototype.getIndices = function(obj){
    var indices = new Array();
    var i = this.length;
    while (i--) {
        if(this[i] === obj){
            indices.push(i);
        }
    }
    return indices;
}

Array.prototype.Union = function(arr){
    //combines two arrays together to return a single array containing all elements (once)
    //{1,2,3,4,5}.Union({3,4,5,6,7})
    //returns: {1,2,3,4,5,6,7}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            primArray.splice(i, 1);
        }
    }
    var returnArr = primArray.concat(secondArray);
    return returnArr;
}

Array.prototype.Intersection = function(arr){
    //Returns an array of elements that are present in both sets
    //{1,2,3,4,5}.Intersection({3,4,5,6,7})
    //returns: {3,4,5}
        var primArray = this;
        var secondArray = arr;
    var returnArr = new Array;
    var i = 0;
    while(i++<primArray.length){
        if(secondArray.contains(primArray[i])){
            returnArr.push(primArray[i]);
        }
    }
    return returnArr;
}

Array.prototype.Complement = function(arr){
    //Returns an array of elements that are only in the primary (calling) element
    //{1,2,3,4,5}.Complement({3,4,5,6,7})
    //return: {1,2}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            primArray.splice(i, 1);
        }
    }
    return primArray;
}

Array.prototype.SymmetricDifference = function(arr){
    //Returns elements that are exclusive to each set
    //{1,2,3,4,5}.SymmetricDifference({3,4,5,6,7})
    //return: {1,2,6,7}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            var indices = secondArray.getIndices(primArray[i]);
            primArray.splice(i, 1);
            var j=indices.length;
            while(j--){
                secondArray.splice(indices[j], 1);
            }
        }
    }
    var returnArr = primArray.concat(arr);
    return returnArr;
}

function run(){
     var Q = "A";
     var D = [1,2,3,4,5,6,7,8,9,10];
     var sets = {
          "A":[1,2,3],
          "B":[3,4,5],
          "C":[5,6,7]
     }
     var R = D;
     for(var el in sets){
          R = R.Complement(sets[el]);
     }
//if I alert D at this point I get 8,9,10 instead of 1,2,3,4,5,6,7,8,9,10 as I would expect? What am I missing here... It causes a problem when I perform D.Complement(R) later on
     document.write(R + "<br/>");
     R = R.Union(sets[Q]);
     document.write(R + "<br/>");
  //Here!  
     R = D.Complement(R);
     document.write(R);
}

</script>

</head>

<body onload="run()">

</body>
</html>

Everything is working up to the final point when I then try to get the complement of the domain and my newly constructed set. I am expected to be getting the complement of [1,2,3,4,5,6,7,8,9,10] and [8,9,10,1,2,3] which would yield [4,5,6,7] but when I perform D.Complement(R) my D variable seems to have turned into [1,2,3]. This appears to happen after the enumeration I perform.

I thought it might be because I was using this.splice and arr.splice in my functions and when I was passing the variables to the functions they were being passed as pointers meaning I was actually working on the actual memory locations. So I then used primArray and secondArray to create a duplicate to work on… but the problem is still happening

Many Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:36:34+00:00Added an answer on May 18, 2026 at 8:36 pm

    So I then used primArray and secondArray to create a duplicate to work on… but the problem is still happening

    Just assigning it to a variable does not make it a new array, you are still working on the array that was passed in. You have to manually make a new copy of the array either by looping through it and copy each index or by joining and splitting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was fed up with the limited javascript Array functions and wanted to write
My application is fed data from an external device. After each data point, there
I have a UITableViewController fed by an NSFetchedResultsController. From it, the user can call
I'm not asking to be spoon fed here, just need some pointers on where
I have a rather old website that I've fed up refactoring so I'm rebuilding.
I am trying to understand how string1/string2 in the block are being fed input
I need file_get_contents to be fault tolerant, as in, if the $url fed to
I would like to create a method that returns an XmlReader. Depending on the
Let's say we have two files. match.txt : A file containing patterns to match:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.