
Likewise, getIndexToIns(, 19) should return 2because once the array has been sorted it will look like and 19 is less than 20 (index 2) and greater than 5 (index 1). The method findIndex is used to return the index of the first array element that passes a certain test function.

For example, getIndexToIns(, 1.5) should return 1because it is greater than 1 (index 0), but less than 2 (index 1). Algorithm instructions Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. We have to sort an array of numbers from least to greatest and find out where a given number would belong in that array. This challenge gives us a glimpse into the wonderful world of sorts. There are all kinds of sorts: bubble sort, shell sort, block sort, comb sort, cocktail sort, gnome sort - I’m not making these up! Deleted elements are visited as if they were undefined.Sorting is a very important concept when writing algorithms. If an existing, yet-unvisited element of the array is changed by callbackFn, its value passed to the callbackFn will be the value at the time that element gets visited.Changes to already-visited indexes do not cause callbackFn to be invoked on them again.callbackFn will not visit any elements added beyond the array's initial length when the call to findIndex() began.Note, however, that the length of the array is saved before the first invocation of callbackFn. Empty slots in sparse arrays behave the same as undefined.įindIndex() does not mutate the array on which it is called, but the function provided as callbackFn can. If callbackFn never returns a truthy value, findIndex() returns -1.ĬallbackFn is invoked for every index of the array, not just those with assigned values. findIndex() then returns the index of that element and stops iterating through the array. It calls a provided callbackFn function once for each element in an array in ascending-index order, until callbackFn returns a truthy value.

