Implementing Insert Function

前端 未结 5 1689
感情败类
感情败类 2020-12-11 17:40

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

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 18:18

    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.

提交回复
热议问题