Where i belong; I don´t know if this code is ok for the problem any advice?


function getIndexToIns(arr, num) {
let count = 0;
for (let i = 0; i < arr.length; i++)
if (arr[i] < num) {
count++;
}
}
return count;
}

getIndexToIns([40, 60], 50);

Hello and welcome!

First. You can try to edit your post and press ctrl-e. You will see how to make your code look more readable.

Second. We don’t know, for what purpose this code is. Please provide challenge step link if it’s from freeCodeCamp curriculum.

If not, describe the problem. What this code should do?

1 Like

Your current code would only work for the Where do I Belong challenge if all the arrays passed to the function were already sorted. The tests have arrays that are not sorted, so you will need to add some additional code to make this work.

1 Like

Looks good to me!

Since you have no need for the index variable i, you could use a for…of loop to make this a little more readable.

for (const val of arr) {
  if (val < num) {
    count++;
  }
}

it’s just checking how many elements are smaller than num, that doesn’t rely on order

I think I stayed up too late last night. My brain must have been foggy.