返回一串带空格的数字中的最高和最低数字

Sca*_*ola 2 javascript numbers

假设我有一串由空格分隔的数字,我想返回最高和最低的数字。使用函数在 JS 中如何最好地完成?例子:

highestAndLowest("1 2 3 4 5"); // return "5 1"
Run Code Online (Sandbox Code Playgroud)

我希望这两个数字都以字符串形式返回。最小的数字先跟一个空格,然后是最大的数字。

这是我到目前为止所拥有的:

function myFunction(str) {
    var tst = str.split(" ");
    return tst.max();
}
Run Code Online (Sandbox Code Playgroud)

Wal*_*anG 5

您可以使用 Math.min 和Math.max,并在数组中使用它们来返回结果,请尝试:

function highestAndLowest(numbers){
  numbers = numbers.split(" ");
  return Math.max.apply(null, numbers) + " " +  Math.min.apply(null, numbers)
}

document.write(highestAndLowest("1 2 3 4 5"))
Run Code Online (Sandbox Code Playgroud)