Why my code is wrong?

help me why it’s wrong?

Your code so far

function getIndexToIns(arr, num) {
arr = arr.sort((a, b) => a-b);
for (let i in arr) {
  if(num<=arr[i]) return i;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:80.0) Gecko/20100101 Firefox/80.0.

Challenge: Where do I Belong

Link to the challenge:

Hello @ngos,

Welcome to the community.

You are trying to iterate the array while keeping track of the index of its elements.

In your code, you used the for (let x in arr) method to iterate the array. This method is used to iterate over values of properties in an object. Rather use for let x of arr and note that x in this case is the element itself not its index. I will suggest you use a for loop to iterate the array.


for(let [i, val] of arr.entries()) {
   // i is the index and val is the value at that index

The best way to find the index in an array whose element meets a criteria is using the findIndex() array method. You can check this link to find out more… www.freecodecamp.org/news/javascript-standard-objects-arrays/amp/