Tell us what’s happening:
What are the problems/mistakes that are on my code?
There are test cases that did not pass Why?
The test cases that did not pass the challenge :
getIndexToIns([10, 20, 30, 40, 50], 30) should return 2.
getIndexToIns([3, 10, 5], 3) should return 0.
getIndexToIns([5, 3, 20, 3], 5) should return 2.
getIndexToIns([2, 5, 10], 15) should return 3.
getIndexToIns([2, 5, 10], 15) should return a number.
getIndexToIns([], 1) should return 0.
getIndexToIns([], 1) should return a number.
Your code so far
function getIndexToIns(arr, num) {
let newSortedArr = arr.sort((num1, num2) => num1 - num2);
for (let i = 0; i < newSortedArr.length; i++) {
if (newSortedArr[i] > num) {
return newSortedArr = newSortedArr.indexOf(newSortedArr[i]);
}
}
return newSortedArr;
}
console.log(getIndexToIns([2, 5, 10], 15));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:83.0) Gecko/20100101 Firefox/83.0.
it’s solvable from this point, you will need to change/add a few things tho.
so, what to do when all the elements in the array are smaller than the number to insert? what’s the index then? (in this case, what index should have been returned?)
In the case that all elements are smaller than the items in the array, none of the if-statements in the for loop are ever true. So the only thing that returns from the function is that very last return total. Right now it returns 0, because you’ve initialised it with 0, but does it have to be 0?
Does that help enough for you to solve it from here?