Where do I Belong helpp

Where do I Belong helpp
0

#1
getIndexToIns([2, 5, 10], 15) should return 3

there are only 3 numbers how am i supposed to return 3

Your code so far


function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr.sort(function(a, b){return a-b});
  for(let i = 0;i<arr.length;i++)
  {
if (num <=arr[i])
{num = i;
break;}

  }
  

  }
  return num;
}

getIndexToIns([40, 60], 50);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong


#2

From the instructions:

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted.

You are imagining that you are inserting the number so the array will grow by one.


#3

do i have to push the second arg into the array first?


#4

No, you are just returning the index. You are imagining that you inserting it. And if the number is larger than anything in the array, it will go at the end of the array, which would be one larger than the original.


#5
function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr.sort(function(a, b){return a-b});
  for(let i = 0;i<arr.length;i++)
  {
if (num <=arr[i])
{num = i;
break;}
else{
  arr.push(num);
  
}



  }
  
return num;
  }

shouldnt i be passing this test too with this code?

getIndexToIns([], 1) should return 0.

#6

OK, first of all, for the love of all that is good in the world, please format your code as you go. Messy code is hard to understand and therefore hard to debug. Just make it a habit.

function getIndexToIns(arr, num) {
  arr.sort(function(a, b) {
    return a - b;
  });
  for (let i = 0; i < arr.length; i++) {
    if (num <= arr[i]) {
      num = i;
      break;
    } else {
      arr.push(num);
    }
  }
  return num;
}

Now that I can read your code, I can tell you the problem. Try the test case:

console.log(getIndexToIns([], 127))
// 127

Can you follow the code along? If it’s an empty array, the num = i; never gets run.

One solution would be to put some logic at the beginning - if it’s an empty array, return 0.

The other point I would say is to get rid of the push - we don’t have to add to the array at all. It’s asking up where it would go.

I also don’t think we need to do anything to num.

Think about your indexing variable. Once you break you can just send that return that. Of course i would have to have scope at the return statement so either declare and initialize it before the for loop or use var. If you do that, you don’t need to check for an empty array - it will just skip the for loop and return 0.


#7

so if its an empty array it wont run the for loop,so i should declare i with var in the beginning with a conditional that it returns 0 if it is empty


#8

Either declare it ar the for loop as a var or before the loop so it has scope at the return. Personally, I try to avoid using var so I would probably go with the latter, but in this case they do the same thing.