I was working on this puzzle, and I got a working solution for almost all cases:
function getIndexToIns(arr, num) {
arr.sort(function(a, b) {
return a - b;
});
for (var i = 0; i < arr.length; i++) {
if (arr[i] >= num)
return i;
}
}
I got a bit fed up, so I checked the solution: I see that, to deal with the other scenarios, the proposed solution returns “arr.length”. I don´t understand why, though. Could anyone explain?