This code works correctly but

Tell us what’s happening:

This code works correctly but it doesn’t pass the test. I don’t understand why it happens.

Your code so far


function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort((a, b) => a - b);
for (let elem in arr) {
  if (num <= arr[elem]) {
    console.log(elem + " " + arr[elem])
    return elem;
  }

}

}

getIndexToIns([5, 3, 20, 3], 5);

Your browser information:

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

Challenge: Where do I Belong

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong

You’re using a for...in loop, which is intended to iterate over keys in an object. Because object keys are strings, your elem is a string and therefore does not pass the tests.
I suggest reading more about for...in loops:

Especially look at this bit:

1 Like

Thank you so much ArielLeslie. I’ll keep that in mind. :hugs: :wink: