Please help me I’ve spent two hours unsuccessfully debugging this one case.

I used a recursive function to do this problem. It makes sense to do it this way as you can scale it. A for loop would go through the array one by one. My recursive function splits the array in two, puts the number in between, checks if it fits, if it’s to big cuts the second half in half and sticks it between those and repeats and if it’s too small cuts the first half in half… So less work than a for loop. And my code logically works!

But this ONE PARTICULAR CASE when the number has to fit at the end of the array, like a .push() method. Why doesn’t it work? Let’s go through it step by step.

- 15 can’t fit between 2 and 5 so put it between 5 and 10.
- 15 can’t fit between 5 and 10 so put it after 10.
- The index now turns this statement falsey:
`currentIndex > 0 && currentIndex < sortedArr.length`

- The function should then skip the if statement and return currentIndex, or what the index would be after 10, or 3. The index is three. YET IT RETURNS 2 HOW CAN THIS BE?

```
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var sortedArr = arr.sort(function(a,b){
return a-b;
});
function recursive(currentIndex){
if(currentIndex > 0 && currentIndex < sortedArr.length){
if(num > sortedArr[currentIndex-1] && num <= sortedArr[currentIndex]){
return currentIndex;
} else if (num <= sortedArr[currentIndex-1]){
currentIndex = Math.floor(currentIndex/2);
recursive(currentIndex);
} else {
currentIndex = Math.round(currentIndex + currentIndex/2);
recursive(currentIndex);
}
}
return currentIndex;
}
return recursive(Math.floor(sortedArr.length/2));
}
getIndexToIns([2, 5, 10], 15);
```

Google Chrome Version 65.0.3325.181 (Official Build) (64-bit)

Your Browser User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36`

.

**Link to the challenge:**