Algorithms: Implement Merge Sort

Hi FCC,
So I have followed guide and solution, which made sense and it is still not working.
I have tried going over it and I don’t understand what is wrong.

I was examining the solution code because I was stuck

Code is

``````//Merger function, which merges 2 sorted array into 1 sorted array
function merger(arr1, arr2){
let i = 0, j = 0, mergedArr = [];
while (i < arr1.length && j < arr2.length){
if (arr1[i] > arr2[j]) mergedArr.push(arr2[j++]);
else mergedArr.push(arr1[i++]);
}
while (i < arr1.length){
mergedArr.push(arr1[i++]);
}
while (j < arr2.length){
mergedArr.push(arr2[j++]);
}
return mergedArr;
}
function mergeSort(array) {
//Array of length 1 is sorted so we return the same array back
if (array.length == 1) return array;

//Break down the array to half from middle into left and right
let middle = Math.floor(array.length/2);
let left = mergeSort(array.slice(0, middle));
let right = mergeSort(array.slice(middle));

//Return the merged sorted array
return merger(left, right);
}
``````

When I test it it shows me an error

``````// running tests
mergeSort should not use the built-in .sort() method.
// tests completed
``````

My guess is that there is imprecision in the test. If you remove the comments that have “sort” in them, does it still say that?

That did it!
Thanks Ariel!

If you’ve got a minute, it would be helpful to search through the GitHub Issues to see if it’s already been reported. If not you can report that the tests break with “sort” in the comments.

Just did

Awesome! Thanks.

Happy coding.