0

I very new to JavaScript. And wrote the program where I need to compare arrays (lists). And JavaScript make it wrong. it compares it like strings:

[ 1, 13, 14, 11, 7 ] > [ 1, 3, 14, 12, 11 ] Woulb be FALSE in JS, because it compares 1 with 3 when it comes to second item. but I need it to compare whole number, not a part of it.

My program was later on Python, so there [ 1, 13, 14, 11, 7 ] > [ 1, 3, 14, 12, 11 ] is TRUE

Merlin
  • 24,552
  • 41
  • 131
  • 206
  • 1
    Possible duplicate of [How to check if two arrays are equal with JavaScript?](http://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript) – CodingWithSpike Jun 14 '16 at 02:34
  • Another possible duplicate: http://stackoverflow.com/questions/22395357/how-to-compare-two-arrays-are-equal-using-javascript-or-jquery – CodingWithSpike Jun 14 '16 at 02:38
  • There is no `[] > []` or `[] < []` built into javascript. Even `[] == []` only checks if they are same object reference, not actual elements in array. So `[1,2] == [1,2]` is false – charlietfl Jun 14 '16 at 02:43

2 Answers2

0

I think this works :

function AgreaterB(a,b) {
  sumA = a.reduce((pv, cv) => pv+cv, 0);
  sumB = b.reduce((pv, cv) => pv+cv, 0);
  return sumA > sumB;
}

just use it this ways :

AgreaterB([ 1, 13, 14, 11, 7 ], [ 1, 3, 14, 12, 11 ]);

or

arrA = [ 1, 13, 14, 11, 7 ];
arrB = [ 1, 3, 14, 12, 11 ];
AgreaterB(arrA, arrB);
Sherly Febrianti
  • 1,097
  • 15
  • 33
0

JavaScript does not have built in > or < operators for Arrays. But you can define them yourself for Arrays.

Greater than

// attach the .greaterThan method to Array's prototype to call it on any array
Array.prototype.greaterThan = function (array) {
    // if the other array is a false value, return
    if (!array)
        return false;

    var minArrayLength = Math.min(this.length, array.length);

    for (var i = 0; i < minArrayLength; i++) {
        if (this[i] > array[i]){
            return true;
        }
    }
    return false;
};

// Hide method from for-in loops
Object.defineProperty(Array.prototype, "greaterThan", {enumerable: false});

Usage

[1, 2, 3, 4].greaterThan([1, 2, 3, 6]) === false;
[1, 13, 14, 11, 7].greaterThan( [1, 3, 14, 12, 11]) === true;

Less Than

// attach the .lessThan method to Array's prototype to call it on any array
Array.prototype.lessThan = function (array) {
    // if the other array is a false value, return
    if (!array)
        return false;

    var minArrayLength = Math.min(this.length, array.length);

    for (var i = 0; i < minArrayLength; i++) {
        if (this[i] > array[i]){
            return false;
        }
    }
    return true;
};

// Hide method from for-in loops
Object.defineProperty(Array.prototype, "lessThan", {enumerable: false});

Usage

[1, 2, 3, 2].lessThan([1, 2, 3, 4]) === true;
[1, 13, 14, 11, 7].lessThan( [1, 3, 14, 12, 11]) === false;
Kairat
  • 790
  • 3
  • 7
  • Actually, JavaScript does apparently have built in > and < operators for arrays. At least as far as I have tested in Chrome, Firefox, and various Node.js versions just now, and as the OP obviously used here to show his problem with them. Sadly, I can't find any documentation about what exactly happens within them. (The only sparse info on MDN is actually wrong and in contradiction with actual Firefox behaviour.) So far, from my experiments, I believe what happens is an element-wise lexical comparison in order (after coercion to string for each element). – trollkotze Nov 05 '17 at 07:21