https://www.freecodecamp.com/challenges/where-do-i-belong
The challange description is wrong as it states : Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
A simple [20,3,5].sort() will reveal that the sorted array is not [3,5,20] as presented in description but [20,3,5] . This will invalidate all challange description and tests I presume.
// If relevant, paste all of your challenge code in here
The simpliest way to sort and be sure it sorts ascending is this :
var aux;
for(i=0;i<arr.length-1;i++){
for(j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
aux = arr[i];
arr[i] = arr[j];
arr[j] = aux;
}
}
}
@arizona2014 thanks for the issue but there are no errors in the challenge description. If want to use the .sort method, I would re-read the .sort link. But the challenge doesn't prevent you from writing your own sorter as you have done so yourself. Happy coding!
You're right Eric ... I read the sort documentation before submitting the issue, but for some reason didnt make sense then. Looked again now and arr.sort(function(a, b){return a-b}) should return the same thing as my snippet. Thanks
Most helpful comment
@arizona2014 thanks for the issue but there are no errors in the challenge description. If want to use the
.sortmethod, I would re-read the.sortlink. But the challenge doesn't prevent you from writing your own sorter as you have done so yourself. Happy coding!