Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top