Hi
I’m having some trouble with this challenge…
My code so far passes all the tests except one (see commented code). Can anyone advice whether my code is legible to begin with? Perhaps the approach I am using is causing the one test to fail.
function sorter(a, b){
return a - b;
}
function getIndexToIns(arr, num) {
// sort the array
// find where num fits in
// return that index
console.log(arr.sort(sorter));
arr = arr.sort(sorter);
for (let i = 0; i < arr.length; i++) {
if (arr[i] >= num) {
num = arr.indexOf(arr[i])
console.log(num);
return num;
}
}
return 0;
}
getIndexToIns([2, 5, 10], 15); //fail!
getIndexToIns([40, 9, 60, 3, 64, 9, 8], 50); //pass
getIndexToIns([10, 20, 30, 40, 50], 35); //pass
getIndexToIns([10, 20, 30, 40, 50], 30); //pass
getIndexToIns([40, 60], 50); //pass
getIndexToIns([3, 10, 5], 3); //pass
getIndexToIns([5, 3, 20, 3], 5); //pass
getIndexToIns([2, 20, 10], 19); //pass
getIndexToIns([], 1); //pass
I tried adding an else-if statement where num > arr[i] but that didn’t resolve it. Just need some advice/a hint on what I could do to solve the challenge, it would be much appreciated.