获取最大(或最高)的字母

cta*_*mli 1 javascript

如果我们想获得最大值或最小值,我们可以使用Math.maxMath.min。我想知道是否有办法获得最高的字母。

例如;

var i = 'A';
var ii = 'B';
var iii = 'C';

result = getthemax(i , ii, iii);   
// so result equels to 'C'
Run Code Online (Sandbox Code Playgroud)

我搜索了很多,但找不到这样的东西。有什么办法可以做到这一点吗?

Joe*_*e50 5

您可以简单地将它们放入数组中,然后使用Array.sort()数组.pop()中的最后一项:

var i = 'A';
var ii = 'B';
var iii = 'C';

var highestLetter = [i, ii, iii].sort().pop(); //will now be "C".
Run Code Online (Sandbox Code Playgroud)

执行此操作的函数是:

function getthemax() {
    return Array.prototype.pop.call(Array.prototype.sort.call(arguments));
}
getthemax(i, ii, iii); //"C"
Run Code Online (Sandbox Code Playgroud)