Correct: `Array.from()` (it’s a static method) Incorrect: `Array.join()` (doesn’t exist; it’s a prototype method) This patch uses the common `#` syntax to denote `.prototype.`.
20 lines
583 B
Markdown
20 lines
583 B
Markdown
### sortedIndex
|
|
|
|
Returns the lowest index at which value should be inserted into array in order to maintain its sort order.
|
|
|
|
Check if the array is sorted in descending order (loosely).
|
|
Use `Array.prototype.findIndex()` to find the appropriate index where the element should be inserted.
|
|
|
|
```js
|
|
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;
|
|
};
|
|
```
|
|
|
|
```js
|
|
sortedIndex([5, 3, 2, 1], 4); // 1
|
|
sortedIndex([30, 50], 40); // 1
|
|
```
|