0
2
JHJannick Holm
The sortedIndex function uses the built-in Array.findIndex function to get an index that indicates the location of the element that is the lowest value in the array. If the value at that location is lower than the value of the element at the first position in the original array, the index is negative one, indicating that the value at that location should be moved down one position in the array. If the value at the location is higher than the value of the element at the first position in the original array, the index is positive one, indicating that the value at that location should be moved up one position in the array.
const sortedIndex = (arr, n) => {
const isDescending = arr[0] > arr[arr.length - 1];
const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));
return index === -1 ? arr.length : index;
};