UNPKG

@samnarduzzi/lotide

Version:

Lotide proejct containf files, test code files, index, and descriptions for all files

17 lines (15 loc) 779 B
const assertArraysEqual = require('./assertArraysEqual'); const middle = function(array) { if (array.length <= 2) { return []; // empty output for arrays that are less than 3 or <= 2 } else if (array.length % 2 === 0) { return [array[array.length / 2 - 1], array[array.length / 2]]; // for even arrays: returns first element at index (array.length / 2 - 1 ex. [1,2,3,4] --> 4/ 2 - 1 = 1, 4/ 2 = 2 --> returns 1st and 2nd index aka [2],[3] // and then returns second element at index (array.length / 2) } else { return [array[Math.floor(array.length / 2)]]; // for odd arrays: divides array length and rounds down to nearest interger ex. [1,2,3] --> 3/ 2 = 1.5 , round down to 1 --> returns 1st index aka [2] } }; module.exports = middle;