javascript - Implementing Insert Function -
i working through khan academy's algorithm course, uses js teach fundamental algorithms. in process of implementing insertion sort, have found problem.
we writing function takes in array, index start , value, in order insert number in correct ordered position. have written said function here:
var insert = function(array, rightindex, value) { (var = rightindex; array[i] >= value; i--) { array[i+1]=array[i]; array[i] = value; } return array; };
this works fine, , performs should, not pass ka's automated marking system. give guidelines code , suggest done such:
for(var ____ = _____; ____ >= ____; ____) { array[____+1] = ____; } ____;
does know how reiterate code conform these standards?
i had similar solution , didn't pass automated test. if later @ "challenge: implement insertion sort" go ahead , implement function you:
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; };
as aside, reason don't need declare j before loop (to used later) because javascript doesn't have block scope (til): see here
Comments
Post a Comment