curriculum/challenges/english/blocks/lab-sorted-index-finder/68a47dcdbce12193caf77012.md
In this lab you will create a function that returns the lowest index at which a value should be inserted into an array once it has been sorted in ascending order.
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
getIndexToIns function that takes two arguments: an array and a number.sort method to sort the array in ascending order.getIndexToIns function should return the lowest index at which the number should be inserted by using the findIndex method.getIndexToIns function should always return a number.Hint:
The findIndex method is a built-in array method in JavaScript. It takes a callback function and returns the index of the first element that satisfies the condition. Both findIndex and sort are higher-order functions.
Examples:
getIndexToIns([1, 2, 3, 4], 1.5) should return 1 because 1.5 is greater than 1 (index 0) and less than 2 (index 1).getIndexToIns([20, 3, 5], 19) should return 2 because after sorting to [3, 5, 20], 19 is less than 20 (index 2) and greater than 5 (index 1).You should have a getIndexToIns function.
assert.isFunction(getIndexToIns);
getIndexToIns should always return a number.
assert.isNumber(getIndexToIns([10, 20, 30, 40, 50], 35));
Your function should use the sort method.
assert.match(getIndexToIns.toString(), /\.sort\(/);
Your function should make use of the findIndex method.
assert.match(getIndexToIns.toString(), /\.findIndex\(/);
getIndexToIns([10, 20, 30, 40, 50], 35) should return 3.
assert.strictEqual(getIndexToIns([10, 20, 30, 40, 50], 35), 3);
getIndexToIns([10, 20, 30, 40, 50], 30) should return 2.
assert.strictEqual(getIndexToIns([10, 20, 30, 40, 50], 30), 2);
getIndexToIns([40, 60], 50) should return 1.
assert.strictEqual(getIndexToIns([40, 60], 50), 1);
getIndexToIns([3, 10, 5], 3) should return 0.
assert.strictEqual(getIndexToIns([3, 10, 5], 3), 0);
getIndexToIns([5, 3, 20, 3], 5) should return 2.
assert.strictEqual(getIndexToIns([5, 3, 20, 3], 5), 2);
getIndexToIns([2, 20, 10], 19) should return 2.
assert.strictEqual(getIndexToIns([2, 20, 10], 19), 2);
getIndexToIns([3, 10, 5], 11) should return 3
assert.strictEqual(getIndexToIns([3, 10, 5], 11), 3);
getIndexToIns([], 5) should return 0
assert.strictEqual(getIndexToIns([], 5), 0);
function getIndexToIns(arr, num) {
const sorted = [...arr].sort((a, b) => a - b);
const index = sorted.findIndex(el => el >= num);
return index === -1 ? sorted.length : index;
}