Tell us what’s happening:
I implemented this based on MZ sort()
. It is a valid solution, but I don’t get the anonymous function taken as a sort argument: what’s the point of substracting one number from other over and over again. I already saw it in a debugger and what I saw was that if there was a positive result a and b were sorted, otherwise (negative number as result) nothing is sorted and the function goes on…
MZ explains the function in this way: " To compare numbers instead of strings, the compare function can simply subtract b
from a
. The following function will sort the array in ascending order (if it doesn’t contain Infinity
and NaN
):".
Your code so far
function getIndexToIns(arr, num) {
let numIndex;
arr.push(num);
arr.sort(function(a, b){
return a - b;
})
numIndex = arr.indexOf(num);
return numIndex;
}
getIndexToIns([5, 3, 20, 3], 5);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.94
.
Link to the challenge: