"Where do I belong" JS exercice


My code seems fine, but Free Code Camp tells me it is not good for values


My code returns 3 which is correct, but Free Code Camp says it should return the number 3 for these values, which it does!

What is wrong with my code?


function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  for (var i=0;i<arr.length;i++) {
    if (arr[i] >= num) {
      num= i;
      return num;
    else if (num>= arr[i]) {
      return (arr.length);



When I run your code it passes the test for getIndexToIns([2,5,10],15); (returning 3), but it does fail several other tests.

First of all you have to sort the array because not every time sorted array will be passed as parameter.
Secondly, you just need to provide the return statement when the array element is greater than or equals to the element to compare the position at which it would insert. Then after that if the loop executed completely you have to return the arrays length i.e element should be inserted at the end of the array.

Here is the image of your code after editing.