@b1u3too/lotide
Version:
LHL Web Development Flex Program Project -- Helper function library inspired by lodash
17 lines (12 loc) • 330 B
JavaScript
const middle = function (inputArray) {
if (inputArray.length <= 2) {
return [];
}
const middleIndex = Math.ceil(inputArray.length / 2) - 1;
if (inputArray.length % 2 !== 0) {
return [inputArray[middleIndex]];
} else {
return inputArray.slice(middleIndex, middleIndex + 2);
}
}
module.exports = middle;