Reputation: 811
var gimme = function (inputArray) {
var order = inputArray.slice().sort(function(a,b) { return a-b;});
return inputArray.indexOf(order[1]);
};
This is a function to find the index number of the middle number in a sequence, when given a triplet of numbers. However I don't understand the section:
(function(a,b) { return a-b;});
Could someone explain the purpose of this part? I would be very grateful. Thanks!
Upvotes: 3
Views: 347
Reputation: 16031
This is an example from MDN:
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers);
The result is [1, 2, 3, 4, 5];
So this is a very simple comparator for integers.
Comparators works like the following:
This function uses a simple mathematical property of integers.
Upvotes: 3