Where am I off?

Tell us what’s happening:
I’m not sure what I’m doing wrong here. I took a look at the first solution, and while it’s not exact, (I took an extra step), it seems to line up with what I did. Still I’m not able to pass the test. What am I missing?
Thanks in advance for any help you can offer.

Describe your issue in detail here.

  **Your code so far**

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

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/100.0.4896.127 Safari/537.36

Challenge: Where do I Belong

Link to the challenge:

Hi again!

Your use of return statements is the issue.

Remember that returns will exit the loop and function and return that value.

If this condtion here is not true the first pass of the loop:

 if (newArr[i] >= num) {
    return i;
  }

then the else clause is executed and the function returns the length of that array.

  else {
    return arr.length;
  }

Hope that helps!

1 Like

When I become a rich and famous developer, I’m going to buy you a cape. I can’t tell you the headache this exercise was giving me. Thanks again!

1 Like