You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
440 B

4 years ago
'use strict';
module.exports = function median(key) {
var length = this.items.length;
if (key === undefined) {
if (length % 2 === 0) {
return (this.items[length / 2 - 1] + this.items[length / 2]) / 2;
}
return this.items[Math.floor(length / 2)];
}
if (length % 2 === 0) {
return (this.items[length / 2 - 1][key] + this.items[length / 2][key]) / 2;
}
return this.items[Math.floor(length / 2)][key];
};