Where do I belong

Hi there. I am a bit stuck. Would like an input, where is the error. Thanks

function getIndexToIns(arr, num) {

  let sorting = arr.sort();
  for (let i = 0; i < sorting.length; i++) {
    if (sorting[i] <= num) {
      return sorting.indexOf(sorting[i]);
    }
    if (num > sorting[i]) {
      return sorting.length;
    }
    if (sorting.length === 0) {
      return 0;
    }
  }
}

According to my limited knowledge should be [3, 5, 10].

Test the code and see.

it is [ 10, 3, 5 ]. why???

I think I understood. I just read that numbers are considered strings of characters, so 10 comes before 3 because 1 < 3. Damn, always the syntax…
Thank you for asking me to check, I would had never imagined that.

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