Files
30-seconds-of-code/snippets/sortedIndex.md
Rohit Tanwar e6ce8a054a fixed typo
2017-12-31 20:15:36 +05:30

19 lines
476 B
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### sortedIndex
Returns the lowest index at which value should be inserted into array in order to maintain its sort order
```js
const sortedIndex = (arr,n) => {
arr[0] > arr[1] ? (anarray = arr.reverse(),isReversed = true) : (anarray = arr,isReversed = false);
val = anarray.findIndex( el => {
return n <= el
})
return val === -1 ? arr.length : isReversed ? arr.length - val : val
}
```
```js
sortedIndex([5,3,2,1], 4); // 1
sortedIndex([30,50],40); // 1
```