New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ADD sortedIndex.md] #440
[ADD sortedIndex.md] #440
Conversation
snippets/sortedIndex.md
Outdated
|
||
```js | ||
const sortedIndex = (arr,n) => { | ||
arr[0] > arr[1] ? (anarray = arr.reverse(),isReversed = true) : (anarray = arr,isReversed = false); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
snippets/sortedIndex.md
Outdated
const sortedIndex = (arr,n) => { | ||
arr[0] > arr[1] ? (anarray = arr.reverse(),isReversed = true) : (anarray = arr,isReversed = false); | ||
val = anarray.findIndex( el => { | ||
return n <= el |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There is no need to reverse the array. The following will be faster: 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;
}; |
@atomiks Had the same thoughts, I just fixed these problems! 😆 |
@atomiks I like your approach, it also seems a lot faster! |
@atomiks @Chalarangelo Updated it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job on adding this, thanks yet again for contributing!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks. |
Description
Lodash[ADD] #100 -> https://lodash.com/docs/4.17.4#sortedIndex
What does your PR belong to?
Types of changes
Lodash Backlog
method.md
] or [ADD:method.md
]method
to the lodash backlog.Checklist: