I am currently working through Khan Academy\'s algorithm course, which uses JS to teach fundamental algorithms. I am currently in the process of implementing an insertion so
Most of the answers posted here are correct. But It does not get us to next step in Khan Academy. It could be because Khan Academy expects a certain variable name, indent settings etc. I am not exactly sure why It does not get us to next step.
This code helped me go to next step:
var insert = function(array, rightIndex, value) {
for(var j = rightIndex;
j >= 0 && array[j] > value;
j--) {
array[j + 1] = array[j];
}
array[j + 1] = value;
};
Before I discovered this code, I used i as variable name instead of j, but it did not get me to next step. But this does.