0

First of all, I've checked similar topics on SO such as this one: Comparing similar strings in jquery But they didn't help.

Here's my code:

    //jQuery functions
    (function($)
        {
            arrayToString = function(array)
            {
                var s = "";
                var a = [];
                $.each(array, function(i, el){
                    if($.inArray(el, a) === -1)
                        {
                            a.push(el);
                            if (s!="")
                                {
                                    s += "@~@~@";
                                }
                            s += el;
                        }
                });
                return s;
            };
        })(jQuery);

    (function($)
        {
            intersectionOfArrays = function(a,b)
            {
                var array = [];
                $.each(a, function(i, el){
                    if($.inArray(el, array) === -1 && $.inArray(el, b) != -1) array.push(el);
                });
                return array;
            };
        })(jQuery);    

var intersection = arrayToString(intersectionOfArrays(selectionOf("produitcartesien").split("@~@~@"),tripletsOfCollection.split("@~@~@")));
alert("intersection = '"+intersectionOfArrays(selectionOf("produitcartesien").split("@~@~@"),tripletsOfCollection.split("@~@~@"))+"'");
alert("selectionOf(produitcartesien) = '"+selectionOf("produitcartesien")+"'");
alert("They are different: '"+intersection!=selectionOf("produitcartesien")+"'");

Although the alerts sometimes display the same string, the comparison using != and !== always returns true !?

I tried using some other things that I don't remember, but none worked. How can I modify the above code to get the correct answer?

Thank you in advance.

Community
  • 1
  • 1
Joe
  • 434
  • 1
  • 4
  • 15

2 Answers2

0

Instead of using s != "" your could use if (s.length). When the length is 0 (no content) it will evaluate to false, when the string is filled the length will be > 0 and it will evaluate to true.

Edit:

$.each(array, function(i, el){
                    if($.inArray(el, a) === -1)
                        {
                            a.push(el);
                            if (s.length)
                                {
                                    s += "@~@~@";
                                }
                            s += el;
                        }
                });
JNDPNT
  • 7,445
  • 2
  • 34
  • 40
  • Thank you, but could you be a bit more specific? Some code would be appreciated. :/ – Joe Jan 18 '13 at 09:48
0

Here's what I used to make it work:

(function($)
        {
            arraysAreEqual = function(arr1,arr2)
            {
                if ($(arr1).not(arr2).length == 0 && $(arr2).not(arr1).length == 0)
                    {
                        return true;
                    }
                return false;
            };
        })(jQuery);

var intersection = arrayToString(intersectionOfArrays(selectionOf("produitcartesien").split("@~@~@"),tripletsOfCollection.split("@~@~@")));
alert("intersection = '"+intersectionOfArrays(selectionOf("produitcartesien").split("@~@~@"),tripletsOfCollection.split("@~@~@"))+"'");
alert("selectionOf(produitcartesien) = '"+selectionOf("produitcartesien")+"'");
alert("They are equal: "+arraysAreEqual(intersection.split("@~@~@"),selectionOf("produitcartesien").split("@~@~@")));
Joe
  • 434
  • 1
  • 4
  • 15