[英]Understanding a Javascript Codewars challenge
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!
谢谢!
This is an example from MDN : 这是来自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];
结果是
[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. 此函数使用整数的简单数学属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.