Update sortedIndex.md as suggested by @atomiks
This commit is contained in:
@ -4,12 +4,10 @@ Returns the lowest index at which value should be inserted into array in order t
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const sortedIndex = (arr, n) => {
|
const sortedIndex = (arr, n) => {
|
||||||
arr[0] > arr[1] ? (let anarray = arr.reverse(),let isReversed = true) : (let anarray = arr, let isReversed = false);
|
const isDescending = arr[0] > arr[arr.length - 1];
|
||||||
val = anarray.findIndex( el => {
|
const index = arr.findIndex(el => isDescending ? n >= el : n <= el);
|
||||||
return n <= el
|
return index === -1 ? arr.length : index;
|
||||||
})
|
};
|
||||||
return val === -1 ? arr.length : isReversed ? arr.length - val : val
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
Reference in New Issue
Block a user