Files
30-seconds-of-code/test/sortedLastIndex/sortedLastIndex.js
2018-01-26 20:13:34 +00:00

9 lines
290 B
JavaScript

const sortedLastIndex = (arr, n) => {
const isDescending = arr[0] > arr[arr.length - 1];
const index = arr
.map((val, i) => [i, val])
.reverse()
.findIndex(el => (isDescending ? n <= el[1] : n >= el[1]));
return index === -1 ? 0 : arr.length - index - 1;
};
module.exports = sortedLastIndex