Can't pass last check on adding number to array

Hi everyone! Was really happy that I made it this far with the solution without using the hints/checking the answer.

My code passes all checks except for one; adding a number to an already empty array ( getIndexToIns([], 1) should return 0 .). I can understand why: there is nothing in the array so the number can’t be compared to anything. I tried adding an if else statement for if the array is empty which would push on the number and it allows me to pass on that check but fail on some of the others.

Is there any way to use my solution?

I also don’t understand why in the solution(solution 1), we are returning arr.length. The objective is to return the index of the number which has just been added in. Please help!

  **Your code so far**
function getIndexToIns(arr, num) {
    for (let i = 0; i <= arr.length; i++) {
      arr.sort((a, b) => a - b); 
      if (num > arr[i]) {
        arr.splice([i], 0, num);
    return arr.indexOf(num);
  **Your browser information:**

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

Challenge: Where do I Belong

Link to the challenge:

So the key problem is, that you are REALLY working around to make a simple task super complicated - which I guess is normal if you are starting out, so don’t worry to much :wink:
Also you are calling the sort-function within the loop, which is just wasted time (the array is sorted after the first loop).

Let’s look at the code directly, what do you want to do?

  1. You sort the array.
  2. Then you go through the sorted array and look for a place to insert num so it stays sorted.
  3. Then you insert num there.
  4. Then you call a function to tell you where num is.

Notice how both 1 and 2 are related to sorting while 3 and 4 are related to the index of num? Maybe some of those are not necessary… Maybe switching their position would resolve the problem…

Or maybe, if you are looking for an optimal solution: If you got a sorted array and insert a number, what do you know about the numbers to the left and how does it relate to their index?

1 Like

Yes, it’s definitely very complicated compared to the solutions haha! I’ve been having a tough day with the other exercises so was very proud to have gotten that far with this one. :sweat_smile:

Thanks so much!
Okay, I will move the sort function outside of the loop.
And I definitely don’t need the line where I splice the array.

Do you know why inside the solution loop they return i? I’m confused as to why it’s necessary?

And why are they returning the length of the array instead of the index?

I can usually understand the solution once I read over it but this one has me stumped.

function getIndexToIns(arr, num) {

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

  for (let i = 0; i < arr.length; i++) {

    if (arr[i] >= num)

      return i;


  return arr.length;


So the logic is, that it’s sorting the array, then looking at the number at each index i and once that number is bigger than num, that’s the index where num would be inserted (shifting all the numbers from that index one to the right).
Ofcourse if it is at the end of the array (index = arr.length) it means all numbers were smaller so num would be inserted at the end.
Here the important thing to remember is that the index starts at zero, while length starts at 1 - hence instead of returning the last index+1, it can return the length.

Also keep in mind, “return” exits the function, so it’s either returning i OR it’s returning arr.length if the loop concludes without hitting the return.

Kinda not, but you could use it.
My idea looking at your code was, you could just splice in num somewhere in the array THEN sort it THEN return the indexOf(num) - getting rid of the loop.

There is also an optimal solution without any builtin-functions.

The problem can be solved without sorting the array, simply by counting smaller elements. This is good because if the array would have millions or billions of entries, sorting would take quite some time.

Aah of course. If the number is inserted, it would take the index of the number shifted over (i).

I wouldn’t have remembered that ‘return’ exits the function. Thank you, that makes so much sense!

I’ll try doing it in that order too. It’s all good practice for me. Thank you again :smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.