# Need Help for Basic Algorithm Scripting: Where do I Belong challenge

Tell us what’s happening:
What are the problems/mistakes that are on my code?
There are test cases that did not pass Why?
The test cases that did not pass the challenge :

``````getIndexToIns([10, 20, 30, 40, 50], 30) should return 2.
getIndexToIns([3, 10, 5], 3) should return 0.
getIndexToIns([5, 3, 20, 3], 5) should return 2.
getIndexToIns([2, 5, 10], 15) should return 3.
getIndexToIns([2, 5, 10], 15) should return a number.
getIndexToIns([], 1) should return 0.
getIndexToIns([], 1) should return a number.
``````

``````
function getIndexToIns(arr, num) {
let newSortedArr =  arr.sort((num1, num2) => num1 - num2);
for (let i = 0; i < newSortedArr.length; i++) {
if (newSortedArr[i] > num) {
return  newSortedArr = newSortedArr.indexOf(newSortedArr[i]);
}

}
return newSortedArr;
}
console.log(getIndexToIns([2, 5, 10], 15));
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:83.0) Gecko/20100101 Firefox/83.0`.

Challenge: Where do I Belong

what does this return? what appear in the console for this?

``````[ 2, 5, 10 ]
``````

why is your function returning an array? it should always return a number

I returned a number in `for loop`
see this :

but if it fails “it should return a number”, maybe it doesn’t always work?

what’s the condition for the return?

That is the condition:

look at the input, and at this condition

what values `newSortedArr[i]` takes? what’s `num`?

`newSortedArr[i]` is array’s element
`num` is the number as like 15 in :

look at your condition and look at those numbers, try to write it with all elements in the array

How?
Actually I did not understand what you mean!

`newSortedArr[i]` is each time a different number of 2, 5, 10, and `num` is 15

so you have `2 > 15`, `5 > 15`, `10 > 15` - are those true or false?

They are false as I see

So what to do with these cases?

it’s solvable from this point, you will need to change/add a few things tho.

so, what to do when all the elements in the array are smaller than the number to insert? what’s the index then? (in this case, what index should have been returned?)

I still get stuck on the this challenge.
What I should do when all the elements in the array are smaller than the number to insert?
Here is my code:

``````function getIndexToIns(arr, num) {
let newSortedArr =  arr.sort((num1, num2) => num1 - num2);
let total = 0;
for (let i = 0; i < newSortedArr.length; i++) {
if (newSortedArr[i] > num) {
return  total = newSortedArr.indexOf(newSortedArr[i]);
}
else if (newSortedArr[i] === num) {
}
else if (num === 1) {
}

}
}
console.log(([2, 5, 10], 15));
``````

There is one case that did not pass the test :

``````getIndexToIns([2, 5, 10], 15)
``````

should return

``````3
``````

.

In the case that all elements are smaller than the items in the array, none of the if-statements in the for loop are ever true. So the only thing that returns from the function is that very last `return total`. Right now it returns `0`, because you’ve initialised it with `0`, but does it have to be `0`?

Does that help enough for you to solve it from here?

do you understand why it should return 3?

Yes, `3` Should return the index of the number in the array

No, because I see in the output that `return total;` returns `15`
I don’t know why?