• Mission [Median] fails to sort the 5th array

 

I would like to give some feedback about ...

From: https://js.checkio.org/mission/median/solve/

HTTP_USER_AGENT:

Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0

Code fails to sort the 5th array. The array when sorted is [ 5, 0, 1, 2, 3, 4, 6, 7, 8, 9, 10 ].

This code sorts the 5th array correctly in jsfiddle.net.

"use strict";

function median(data) {
    const sortedArray = data.sort((a,b) => a > b);

    if(sortedArray.length % 2 !== 0) {
        return sortedArray[Math.floor(sortedArray.length / 2)];
    } else {
        const arrayLength = sortedArray.length;
        const a = sortedArray[Math.floor(arrayLength / 2) - 1];
        const b = sortedArray[Math.floor(arrayLength / 2)];
        return ((b - a) / 2) + a;
    }
}

var assert = require('assert');

if (!global.is_checking) {
    assert.equal(median([1, 2, 3, 4, 5]), 3, "1st example");
    assert.equal(median([3, 1, 2, 5, 3]), 3, "2nd example");
    assert.equal(median([1, 300, 2, 200, 1]), 2, "3rd example");
    assert.equal(median([3, 6, 20, 99, 10, 15]), 12.5, "4th example");
    assert.equal(median([10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]), 5, "5th example");
    console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
1