Return 一串带空格的数字中的最高和最低数字
Return highest and lowest number in a string of numbers with spaces
假设我有一串由 space 分隔的数字,我想要 return 最高和最低的数字。如何最好地使用函数在 JS 中完成?示例:
highestAndLowest("1 2 3 4 5"); // return "5 1"
我希望将这两个数字return编辑成一个字符串。最低的数字首先是 space 然后是最高的数字。
这是我目前的情况:
function myFunction(str) {
var tst = str.split(" ");
return tst.max();
}
你可以使用Math.min和Math.max,并在数组中使用它们得到return结果,试试:
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"))
下面是改进方案方便全局使用的代码:
/* Improve the prototype of Array. */
// Max function.
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
// Min function.
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
var stringNumbers = "1 2 3 4 5";
// Convert to array with the numbers.
var arrayNumbers = stringNumbers.split(" ");
// Show the highest and lowest numbers.
alert("Highest number: " + arrayNumbers.max() + "\n Lowest number: " + arrayNumbers.min());
好的,让我们看看如何使用 ES6 制作一个简短的函数...
你有这个string-number:
const num = "1 2 3 4 5";
然后你在 ES6 中创建了一个这样的函数:
const highestAndLowest = nums => {
nums = nums.split(" ");
return `${Math.max(...nums)} ${Math.min(...nums)}`;
}
并像这样使用它:
highestAndLowest("1 2 3 4 5"); //return "5 1"
function highAndLow(numbers){
var temp = numbers.split(' ');
temp.sort(function(a,b){return a-b; });
return temp[temp.length-1] + ' ' + temp[0];
}
有点不同:
先拆分成一个数组,然后排序...并返回最后一个(最大)元素与第一个(最小)元素
函数 highAndLow(数字){
数字 = numbers.split(' ');
return ${Math.max(...numbers)} ${Math.min(...numbers)}
;
}
假设我有一串由 space 分隔的数字,我想要 return 最高和最低的数字。如何最好地使用函数在 JS 中完成?示例:
highestAndLowest("1 2 3 4 5"); // return "5 1"
我希望将这两个数字return编辑成一个字符串。最低的数字首先是 space 然后是最高的数字。
这是我目前的情况:
function myFunction(str) {
var tst = str.split(" ");
return tst.max();
}
你可以使用Math.min和Math.max,并在数组中使用它们得到return结果,试试:
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"))
下面是改进方案方便全局使用的代码:
/* Improve the prototype of Array. */
// Max function.
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
// Min function.
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
var stringNumbers = "1 2 3 4 5";
// Convert to array with the numbers.
var arrayNumbers = stringNumbers.split(" ");
// Show the highest and lowest numbers.
alert("Highest number: " + arrayNumbers.max() + "\n Lowest number: " + arrayNumbers.min());
好的,让我们看看如何使用 ES6 制作一个简短的函数...
你有这个string-number:
const num = "1 2 3 4 5";
然后你在 ES6 中创建了一个这样的函数:
const highestAndLowest = nums => {
nums = nums.split(" ");
return `${Math.max(...nums)} ${Math.min(...nums)}`;
}
并像这样使用它:
highestAndLowest("1 2 3 4 5"); //return "5 1"
function highAndLow(numbers){
var temp = numbers.split(' ');
temp.sort(function(a,b){return a-b; });
return temp[temp.length-1] + ' ' + temp[0];
}
有点不同: 先拆分成一个数组,然后排序...并返回最后一个(最大)元素与第一个(最小)元素
函数 highAndLow(数字){
数字 = numbers.split(' ');
return ${Math.max(...numbers)} ${Math.min(...numbers)}
;
}