I'm reading through the khan academy course on algorithms. I'm at https://www.khanacademy.org/computing/computer-science/algorithms/insertion-sort/p/challenge-implement-insert .
After calling the insert function: * value and the elements that were previously in array[0] to array[rightIndex], should be sorted in ascending order and stored in the elements from array[0] to array[rightIndex+1]. 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.
My attempt is:
var insert = function(array, rightIndex, value) {
var i = rightIndex;
for( array[i]> key ; 0; i-- ) {
array[i + 1] = array[i];
}
array[i]= value;
};
var array = [3, 5, 7, 11, 13, 2, 9, 6];
insert(array, 4, 2);
println("Array after inserting 2: " + array);
They specifically say they want a condition within the for loop, but I don't know how to do that.