'Where do I Belong' Help

'Where do I Belong' Help
0

Hi all,

I’ve written up this code for this challenge and it passes most of the tests except for when this is entered into the function.

getIndexToIns([], 1) should return 0 .

Here’s my code so far.

Your code


function getIndexToIns(arr, num) {
  let sortNumber = (a, b) => a - b;
  return arr.sort(sortNumber).findIndex(arr => arr >= num);
}

console.log(getIndexToIns([], 1));


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

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

Can anyone help explain to me where it’s going wrong?

with this findIndex returns -1 because there is not an element in the array that can satisfy the condition (as the array is empty)
you need to consider this situation separately

Aha, I see. I’ll try to work it more. Thank you. I was confused as to why everything I did would return that.