-1

I am trying to compare two arrays are they equal or not. I am breaking the array a and finally storing them into two different arrays b and c. At last, I am checking the array b and c in the console.

Console shows equal values but when I compare two arrays then I am getting the array are not equal.

Here is my code :

var a = [1,2,3,4,3,2,1];
var b = [];
var c = [];
var t = 0;
var length = a.length;

console.log("is the array length" + length);
if (length %2 !== 0) {
    var mid = parseInt(length/2)-1;
    console.log(a[mid]);
    for(var j=length-1; j>(mid+1); j--) {
        c[t] = a[j];
        t++;
    }
    for(var i=0; i<=mid; i++) {
        b[i] = a[i];
    }
    console.log(c);
    console.log(b);

    if(b == c) { //comparing the array b and c
        console.log("true");
    }
    else {
        console.log("no")
    }
}

Here is my jsbin link : https://jsbin.com/metexuruka/edit

NatNgs
  • 874
  • 14
  • 25
Ashish sah
  • 755
  • 9
  • 17

3 Answers3

3

Depends on your definition of "equal" - if it's that both arrays contain the same elements at the same position, you can use every:

let areEqual = a.length === b.length && a.every((item, index) => b[index] === item);

If you only want to check that they contain the same elements, you can still use every, just without the index check:

let areEqual = a.length === b.length && a.every(item => b.indexOf(item) > -1);
tymeJV
  • 103,943
  • 14
  • 161
  • 157
1

At first, your code to split the array is overly complicated. You could simply slice:

 var a= [1,2,3,4,3,2,1],
 mid = Math.floor(a.length/2),
 b = a.slice(0,mid),
 c = a.slice(mid).reverse();

To compare both arrays, you may create a string ( as you can easily compare strings):

 if(b.join() === c.join()) alert("equal");

Or you iterate and check each:

if( b.length === c.length 
    && b.every((v,i)=> v === c[i])) alert("equal");

If you just want to compare if a is an annagramm, its more easy:

var a= [1,2,3,4,3,2,1];

if( a.every((v,i)=>v===a[a.length-1-i]) ) alert("anagram!");
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

It's too long but you can use this:

<script type="text/javascript">
        /*$(function () {
            $(".nav-item").click(function () {
                $(".nav-item").each(function () {
                    $(this).find("a").removeClass("active");
                });
                $(this).find("a").addClass("active");
            });
        });*/

        // Warn if overriding existing method
        if (Array.prototype.equals)
            console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code.");
        // attach the .equals method to Array's prototype to call it on any array
        Array.prototype.equals = function (array) {
            // if the other array is a falsy value, return
            if (!array)
                return false;

            // compare lengths - can save a lot of time 
            if (this.length != array.length)
                return false;

            for (var i = 0, l = this.length; i < l; i++) {
                // Check if we have nested arrays
                if (this[i] instanceof Array && array[i] instanceof Array) {
                    // recurse into the nested arrays
                    if (!this[i].equals(array[i]))
                        return false;
                }
                else if (this[i] != array[i]) {
                    // Warning - two different object instances will never be equal: {x:20} != {x:20}
                    return false;
                }
            }
            return true;
        }
        // Hide method from for-in loops
        Object.defineProperty(Array.prototype, "equals", { enumerable: false });

        var a = [1, 2, 3, 4, 3, 2, 1];
        var b = []; var c = []; var t = 0;
        var length = a.length;
        alert("is the array length" + length);
        if (length % 2 !== 0) {
            var mid = parseInt(length / 2) - 1;
            alert(a[mid]);
            for (var j = length - 1; j > (mid + 1) ; j--) {
                c[t] = a[j];
                t++;
            }
            for (var i = 0; i <= mid; i++) {
                b[i] = a[i];
            }
            alert(c);
            alert(b);

            if (b.equals(c)) { //comparing the array b and c
                alert("true");
            }
            else
                alert("no");
        }
    </script>