0

It's from Khan Academy challenge: implement insert. I can't seem to move on even though the results are correct.

var insert = function(array, rightIndex, value) {
    var key=value;
    for(var i=rightIndex; key<array[i]&&i>=0; i--)
    {
        array[i+1]=array[i];

    }   
    array[i+1]=key;


};

var array = [3, 5, 7, 11, 13, 2, 9, 6];

insert(array, 4, 2);
println("Array after inserting 2:  " + array);
Program.assertEqual(array, [2, 3, 5, 7, 11, 13, 9, 6]);

insert(array, 5, 9);
println("Array after inserting 9:  " + array);
Program.assertEqual(array, [2, 3, 5, 7, 9, 11, 13, 6]);

insert(array, 6, 6);
println("Array after inserting 6:  " + array);
Program.assertEqual(array, [2, 3, 5, 6, 7, 9, 11, 13]);
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Geek
  • 19
  • 2
  • https://stackoverflow.com/questions/27016443/implementing-insert-function suggests you might need to follow a particular pattern and use the same variable names? – Ry- Jun 04 '17 at 10:06
  • Yep, “Although there are many ways to write this function, you should write it in a way that is consistent with the hint code.” As a first step, just use `value` directly instead of creating a new name `key` that means the same thing. – Ry- Jun 04 '17 at 10:07
  • Natalia, I updated your question, because this is not a "sorting" function, nor is this insertion sort. It's just insert, which is very different. – Patrick Roberts Jun 04 '17 at 10:08

0 Answers0