0

Trying to make this sort function assert true. Not sure why it is not working.

function sortThis() {
    var args = [].slice.call(arguments);

    var myArray = ['Toyota', 'bmw', 'mercedes', 'cadillac', 'Ford', '1Mitsubishi', 'buick'];
    for (var i = 0; i < args.length; i++) {
        myArray.sort();
    }
    return myArray;
}

console.assert( sortThis(['apples', '3mango', 'tomatoes', '1blackberries', 'oranges', 'Peaches']) === ['1blackberries', '3mango', 'Peaches', 'apples', 'oranges', 'tomatoes']);
  • as the answer states: you cannot use === to compare arrays. you *can* (and note: this is a huge hack and I wouldn't personally use it) convert the arrays to strings and compare them. This would only work with very simple arrays. – rlemon Mar 11 '16 at 16:33
  • [Here's a good question on comparing arrays](http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Sterling Archer Mar 11 '16 at 16:34
  • @rlemon `JSON.stringify(myArray)` might handle more cases than just `myArray.toString()`. – Matt Mar 11 '16 at 16:35
  • probably... I still wouldn't use it :P loop over the array and compare values is more sane imo. – rlemon Mar 11 '16 at 16:36
  • isnt it because of the hardcoded value `var myArray = ['Toyota', 'bmw', 'mercedes', 'cadillac', 'Ford', '1Mitsubishi', 'buick'];`, also === doesn't work for arrays – Ammar Hasan Mar 11 '16 at 16:36
  • 1
    @AmmarHasan `[] === []` is false. This is an issue of not being able to compare them. – rlemon Mar 11 '16 at 16:36
  • There is no guarantee that `JSON.stringify` will return the same string when given two different arrays *with the same values*, even if it probably does in all current browser – adeneo Mar 11 '16 at 16:37
  • Also, why is the array sorted inside a loop over the passed in arguments, that makes no sense at all? You have a hardcoded array that you return, it will never have the same values as the array passed in – adeneo Mar 11 '16 at 16:41

3 Answers3

0

It is not possible to compare arrays using === operator. You can compare it with helper function using Array.prototype.every() like this:

function compareArray(arr1, arr2) {
    return arr1.length === arr2.length && arr1.every(function(o, i) {
        return o === arr2[i];
    });
}

Furthermore you are not considering parameter passed to sortThis function. You need to do:

function sortThis(arr) {
    return arr.sort();
}

So now you can assert it:

var actual = sortThis(['apples', '3mango', 'tomatoes', '1blackberries', 'oranges', 'Peaches']);
var expected = ['1blackberries', '3mango', 'Peaches', 'apples', 'oranges', 'tomatoes'];
console.assert(compareArray(actual, expected));
madox2
  • 49,493
  • 17
  • 99
  • 99
  • 2
    … so how can you compare them? You've identified the cause of the problem but not solved it. – Quentin Mar 11 '16 at 16:34
  • I did declare myArray as a parameter in the function and then returned myArray.sort(). Also got rid of the === and used = and that solved the problem. I realize now that I have to pass my variable inside of the function. – Mory Friedman Mar 11 '16 at 16:41
  • @MoryFriedman `=` is not a comparison operator, it is an assignment operator. The result of an assignment to an array is always true. I worry that your tests are not checking the results of your sort at all, but simply returning true since they are asserting that an assignment has happened. – pgoggijr Mar 17 '16 at 15:31
0

You won't be able to compare arrays using the === operator. Instead, you'll have to loop through and compare each element separately, like so:

var sorted = sortThis(['apples', '3mango', 'tomatoes', '1blackberries', 'oranges', 'Peaches'])
var shouldBe = ['1blackberries', '3mango', 'Peaches', 'apples', 'oranges', 'tomatoes']
console.assert(sorted.length === shouldBe.length)
for(var i = 0; i < sorted.length; i++) {
    console.assert(sorted[i] === shouldBe[i])
}

`

pgoggijr
  • 414
  • 1
  • 3
  • 15
-1

To compare two string arrays, simply use [].join, which is very fast as compared to looping and JSON.stringify, because of it's native support.

["a", "sad", "day"].join("") === ["a", "sad", "day"].join("")

here is the snippet for you

result = ["a", "sad", "day"].join("") === ["a", "sad", "day"].join("");

document.write(result === true ? "Assertion Succeeded" : "Assertion Failed");


document.write('<br/>');


result = ["a", "sad", "day"].sort().join("") === ["a", "sad", "day"].join("");

document.write(result === true ? "Assertion Succeeded" : "Assertion Failed");


document.write('<br/>');


result = ["a", "sad", "day"].sort().join("") === ["a", "sad", "day"].sort().join("");

document.write(result === true ? "Assertion Succeeded" : "Assertion Failed");

`

Ammar Hasan
  • 2,436
  • 16
  • 22
  • if you used [1,2] and ["1","2"] as inputs, which are not equal, this approach would fail – pgoggijr Mar 17 '16 at 15:37
  • but as from the input in question, i'ts expected to be an array of strings, therefore this approach will work in context to the question – Ammar Hasan Mar 18 '16 at 14:47