Whats wrong with this code i think it should work 😐

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

console.log(getIndexToIns([], 35));
  **Your browser information:**

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

Challenge: Where do I Belong

Link to the challenge:

Hi @meetb !

I get what you are trying to do here

But that is not the way to go about it.

You basically want to say if there are no items in the array then return 0.

Here is the psuedo code

if(arr.length equals zero){
return zero

Just check if the non sorted array has zero elements and return zero.
It will work with the sorted array too but there is no point in sorting an empty array.

IMO that should be the first condition in your code before you sort the array.

Hope that helps!

Thank you so much once again @jwilkins.oboe. You have always helped me out in all troubles in the most accurate way. [Hope you remember me as I raised many topics and you have solved them :sweat_smile: :sweat_smile:]

1 Like

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