Selection Sort: algorithm works fine but not passing tests

So I programmed a sorting algorithm. It works. The only problem is , it’s not letting me pass the tests. It complains about using the built-in sort method, which I’m clearly not using. Does anyone have an idea why this is. Help would be greatly appreciated*


function selectionSort(arr) {

const len = arr.length 

for (let i = 0 ; i < len; i++) {
  var indexOfmin = i; //Check min of the rest of the array, because  the part of the arr which we already sorted will be ignored
  for (let j = i + 1; j < len; j++)  {
      if (arr[j] < arr[indexOfmin]) indexOfmin = j
  }
 
  if (i !== indexOfmin) {
    //swap 
   [ arr[i],  arr[indexOfmin] ] = [ arr[indexOfmin], arr[i] ];    
  }
}
return arr;
}


selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0.

Challenge: Implement Selection Sort

Link to the challenge:

The algorithm works fine. Console.log(arr) will output this:

[ 1, 1, 2, 2, 4, 8, 32, 43, 43, 55, 63, 92, 123, 123, 234, 345, 5643 ]

Hello there.

If you remove the comment with the word sorted you will pass the tests.

The test algorithm is not robust enough to account for a comment. It just looks for the word.

Hope this helps

1 Like

Well thank you very much. This is something I never would have guessed.