In order to do this, the insert function will need to make room for value by moving items that are greater than value to the right. It should start at rightIndex, and stop when it finds an item that is less than or equal to value, or when it reaches the beginning of the array. Once the function has made room for value, it can write value to the array.
var insert = function(array, rightIndex, value) {
var key = value;
for(var i = rightIndex; array[i] > value ; i = i - 1)
{
array[rightIndex + 1] = array[rightIndex];
}
array[i+1] = value;
};
Why My this function doesn`t work correctly after I input this array !
var array = [3, 5, 7, 11, 13, 2, 9, 6];
It Shows this result:
insert(array, 4, 2);
2,5,7,11,13,13,9,6
<>and create a minimal reproducible example