What is .sort(callbackFunction) ? How does .sort(callbackFunction) work in this code ?
I tried code tracing so parameter a and b in function(a,b) refers to 5 and 3 in arguments ? Is it like that? I am confused.
Your code so far
function getIndexToIns(arr, num) {
arr.sort(function(a, b) {
return a - b;
});
for (var a = 0; a < arr.length; a++) {
if (arr[a] >= num) return a;
}
return arr.length;
}
console.log(getIndexToIns([5, 3, 20, 3], 5));
sort is a built in m,ethod that sort an array using a callback function to compare the elements in the array two by two and ordering based on what the callback returns, saying that the two elements are called a and b, if the caalback returns less than 0 than a comes before b, if the callback returns more than 0 than a goes after b, and if they are equal the callback returns 0
to order numbers a common way is to return their difference as that goes automatically to the more than 0/less than 0 of before