Help with "where do I belong" -

Tell us what’s happening:

I can’t seem to satisfy the requirement [ getIndexToIns([5, 3, 20, 3], 5) should return 2 ] without completely changing up my code. I wonder if I should just add another ternary if statement?..Any help would be appreciated. Thank you!

Your code so far


function getIndexToIns(arr, num) {

return ((arr.findIndex(n => n >= num)) == -1) ? arr.length
: arr.sort().findIndex(n => n >= num);
}

console.log(getIndexToIns([40, 60], 50));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Safari/605.1.15.

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

are you sure sort() is sorting as you want?

you may want to check how it makes the array for the failing test cases

Ah, yes! Thank you!

I fixed it by adding the comparison function [function(a, b){return a - b}] within the sort method.

Why don’t you see if you can solve it without duplicating arr.findIndex(n => n >= num)?