-6

I'm taking a Algorithm class Khan Academy for JavaScript. I wrote a code like this:

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

    var insertionSort = function(array) {
    for (var st = 1; st < array.length; st++) {
        insert(array, st, array[st]);
    }
    };

    var array = [22, 11, 99, 88, 9, 7, 42];
    insertionSort(array);
    println("Array after sorting:  " + array);
    Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);

And now I wanna know what's wrong here, that I can't get into the next level ... Please help. :)

user124942
  • 145
  • 1
  • 9
  • 1
    You'll have to explain a little more. Seems a bit broad your question. – kemicofa ghost Aug 31 '15 at 13:30
  • Hey, I wrote this code to Khan Academy challenge for implement Insertion sort. It looks something isn't right here. Why Khan Academy doesn't take this code for right? – user124942 Aug 31 '15 at 13:33
  • I can't go on the next level. But for me I think nothing is missing to my code. – user124942 Aug 31 '15 at 13:33
  • I have no idea what this Khan Challenge is but you're going to have to update your question and let people know what you want this code to do. Then you'll have to explain which parts seems to be causing a problem. So on and so forth. Debugging questions are not meant for SO btw. – kemicofa ghost Aug 31 '15 at 13:35
  • Still don't know what's wrong here ... This insertionSort returns me a sorted array. – user124942 Aug 31 '15 at 14:05
  • Try this: http://stackoverflow.com/a/36262046/1124594 – haris Mar 28 '16 at 12:12

5 Answers5

11

to all :) It's the right solution. You can't change what is already written.

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; 
};
var insertionSort = function(array) {
for (var st = 1; st < array.length; st++) {
    insert(array, st - 1, array[st]);
}
};
var array = [22, 11, 99, 88, 9, 7, 42];
insertionSort(array);
println("Array after sorting:  " + array);
Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);
user124942
  • 145
  • 1
  • 9
2

The problem wasn't that you were giving a wrong answer, it's that you weren't giving the coding solution they were expecting.

On this particular problem, there's a "hint" section in the upper right hand corner. If you click on the What's this? link.

This hint shows the code you'll need to successfully complete this step, but it is not the complete answer. The empty blanks are parts that you will need to figure out yourself. If you see colored blanks, the values you put in two blanks of the same color must be exactly the same

In their hint, they were expecting the same value be used for the initial var, the for loop and the array. Example: substitute for foo.

var foo;
for(foo = -----; -----; ----){
    array[foo + 1] = -----;
}
----;

The original poster already showed the Khan Academy solution (shown below). Which doesn't match their hint. shrug This code came from a later exercise which included the insert solution.

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; 
};
Rhonda
  • 36
  • 2
  • Reason the hint is expecting 3 colored blanks is probably because they want the variable `j` initialization happens before the `for` loop. So the code matching to the hint would be: ```var j; for (j = rightIndex; j>=0 && array[j] > value; j--) { array[j + 1]= array[j]; } array[j + 1] = value;``` – zenoh Aug 03 '22 at 20:24
1
var insertionSort = function (unsortedList) {
  var len = unsortedList.length;

  for(var i = 0; i < len; i++) {
    var tmp = unsortedList[i]; //Copy of the current element.
    /*Check through the sorted part and compare with the 
    number in tmp. If large, shift the number*/
    for(var j = i - 1; j >= 0 && (unsortedList[j] > tmp); j--) {
      //Shift the number
      unsortedList[j+1] = unsortedList[j];
    }
    //Insert the copied number at the correct position
    //in sorted part.
    unsortedList[j+1] = tmp;
  }
};
var array = [22, 11, 99, 88, 9, 7, 42];
insertionSort(array);
println("Array after sorting:  " + array);
Nitesh singh
  • 915
  • 11
  • 21
0

Your program is right but may be syntax is wrong: use Console.log("Array after sorting: " + array); instead of: println("Array after sorting: " + array); in java Script there is no any method println for output.

Nitesh singh
  • 915
  • 11
  • 21
-1

this works...

var insert = function(array, rightIndex, value) {
    for (var i = rightIndex; i>=0 && array[i] > value; i --) {
        array[i+1] = array[i];
        }
    array[i+1] = value;
};   
  • 5
    It might work, but to make it a useful answer, you need to explain why, and ideally what was wrong with the OP's way of doing things. – Benjamin Dec 05 '15 at 03:54