The code do not pass the test

I tested the code manually and it outputs the correct values

I do not know why my solution is not correct so far


function getIndexToIns(arr, num) {
let newarr = arr.sort(); 
 let result=0;
for(let index in arr){
  if(arr[index]>=num){
     result= index
     return result
  }
}
}

console.log(getIndexToIns(
[10, 20, 30, 40, 50], 30));
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Where do I Belong

Link to the challenge:

According to the docs, for...in should not be used with arrays.

Note: for...in should not be used to iterate over an Array where the index order is important.

It may work as expected in some implementations of JS and not in others.

When I change your for loop to a correct implementation, it works better.

1 Like

Other than that, you have an issue with your sort method. By default, it sorts numbers as strings so it is not sorting the way you think it is. If you look in the docs you will find a discussion of how to deal with numbers.

Other than that, you also have the issue of what to do if the for loop finishes and no element is found greater than num. In other words, what if it is supposed to go at the end? What are you returning in that case?

When I fix those, the code passes for me.

thank you for your help :smiley: this is the first exercise within the track
where I face comptability issues with my code
i have edited my forloop and sort method and what to do when the loop is over

OK … and does it work now?

yes it works thanks for help

1 Like