Untitled
4 years ago in Plain Text
var TEST = function () {
function _getNum(arr, arrInfo) {
if (arrInfo.ptr < arrInfo.len) {
return arr[arrInfo.ptr];
}
return NaN;
}
function _isFirstMin(a, b) {
if (isNaN(a)) { return false; }
if (isNaN(b)) { return true; }
return a <= b ? true : false;
}
return {
exec: function (arr1, arr2) {
let newArr = [];
let newPtr = -1;
let arrInfo1 = { len: arr1.length, ptr: 0 };
let arrInfo2 = { len: arr2.length, ptr: 0 };
let len = arrInfo1.len + arrInfo2.len;
let centerIdxs = [ Math.floor((len-1)/2) ];
centerIdxs.push((len % 2) === 1 ? centerIdxs[0] : centerIdxs[0] + 1);
let isEnough = false;
while (!isEnough) {
let num1 = _getNum(arr1, arrInfo1);
let num2 = _getNum(arr2, arrInfo2);
newPtr++;
if (_isFirstMin(num1, num2)) {
newArr[newPtr] = num1;
arrInfo1.ptr++;
} else {
newArr[newPtr] = num2;
arrInfo2.ptr++;
}
isEnough = newPtr === centerIdxs[1];
}
return (newArr[centerIdxs[0]] + newArr[centerIdxs[1]]) / 2;
}
};
}();