Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4bc229e

Browse files
committed
Where do I Belong 🔍
1 parent 2f1681b commit 4bc229e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,40 @@ function bouncer(arr) {
449449

450450
bouncer([7, "ate", "", false, 9]);
451451
```
452+
453+
### 14. Where do I Belong
454+
455+
_Difficulty: Beginner_
456+
457+
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
458+
459+
For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
460+
461+
Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
462+
463+
---
464+
465+
#### Solution 1
466+
467+
```js
468+
function getIndexToIns(arr, num) {
469+
arr.sort((a, b) => a - b);
470+
471+
for (let i = 0; i < arr.length; i++) {
472+
if (arr[i] >= num) return i;
473+
}
474+
return arr.length;
475+
}
476+
477+
getIndexToIns([10, 20, 30, 40, 50], 35);
478+
```
479+
480+
#### Solution 2
481+
482+
```js
483+
function getIndexToIns(arr, num) {
484+
return arr.filter((val) => num > val).length;
485+
}
486+
487+
getIndexToIns([10, 20, 30, 40, 50], 35);
488+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /