Tell us what’s happening:
when null array is passes, it fails to retun 0, although it is specified.
if(arr.length===0)
return arr.length;
Your code so far
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort(function(a,b) { return a - b; });
console.log(arr.length);
for(var i = 0;i<arr.length;i++)
{
if(arr[i]<num && arr[i+1]>=num)
return i+1;
if(arr[arr.length-1]<num)
return arr.length;
if(arr[i]<=num && arr[i+1]>num)
return i;
if(arr.length===0)
return arr.length;
}
return arr;
}
getIndexToIns([], 1);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong