# Where do I Belong - Help Needed

Where do I Belong - Help Needed
0

#1

This is my code so far

``````function getIndexToIns(arr, num) {
if (arr.length == 0) {
return 0;
} else {
let myArray = arr.sort(function compare(a,b) {
return a - b;
});
function GreaterThan(element) {
return element >= num;
}
return myArray.findIndex(GreaterThan);
}
}
getIndexToIns([10, 20, 30, 40, 50], 35);
``````
``````// running test
getIndexToIns([2, 5, 10], 15) should return 3.
// tests completed
``````

I don’t know why don’t pass when the other ones pass, It return -1.

#2

The findIndex looks for the first element that meets your criteria (the callback returns `true`). If it never sees a match, it returns `-1`. This is the case you are failing, when num is larger than anything in the array.

So, if the num is larger than anything in arr, it will never match so it will return `-1`. Armed with that knowledge, what should the index be?

One solution would be to put in some logic return the correct index if `-1` is returned.

There is another way to solve it, but it would involve changing how you find the index. Either solution is fine.

Also, fyi, if you use the “Ask for help” button, on the challenge, it will post to the forum with links to the forum with links to the challenge so we don’t have to go looking for it.

Stylistically, remember to indent properly - it saves headaches in the long run.

I would also say, that after:

``````  if (arr.length == 0) {
return 0;
}
``````

you don;t need the `else` clause, because you are `return`ing out of the function. Nothing after the `return` will get run anyway. It is very common to do something like:

``````  if (arr.length == 0) {
return 0;
}

let myArray = arr.sort(function compare(a,b) {
//...
``````

It saves you a level of indent which improves readability.

Let us know if this hint is insufficient.

#3

I’m sorry Kevin I didn’t know about that, it’s good to know for the next time.

That hint was more than enough, thank you so much. Now I understand completely what I was doing wrong and how to fix it. Also thank you to remind me that else was unnecessary because I did a return.