Where do I Belong - Please help!

Tell us what’s happening:
Hey everyone,

I can’t find a mistake in my code, don’t know why it doesn’t work. I will be very thankful for your ideas :slight_smile:

Your code so far

function getIndexToIns(arr, num) {
  var newArr = arr.sort();
  var ind = newArr.findIndex(i => i > num);
  return ind >= num ? ind : ind++;
getIndexToIns([40, 60], 50);

Your browser information:

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

Link to the challenge:

I see at least two things wrong with it.

First is your sort function. When you are sorting numbers in an array, default sort function won’t sort correctly because it’s sorting by unicodes. You want to pass in a call back function there.

Second is your findIndex function. If the condition doesn’t meet, it returns false which is index -1. So incrementing by +1 won’t pass one of the test cases.

1 Like

To use sort function and avoid sorting by unicode.

function compareNumbers(a, b) {
  return a - b;

var newArr = arr.sort(compareNumbers);
1 Like

Thanks! It was very helpful!

1 Like

Thank you, I transformed it into an array function, and it worked too!

sort( (a, b) => a - b);