我一直在寻找简单的JS练习,如果你向我展示如何处理这个问题,我会很感激,或者更好的是,提供一个我可以看一看的解决方案。非常感谢。
编辑:我也很欣赏正在使用的函数的任何简单工作示例。
答案 0 :(得分:5)
function whatDidYouTry() {
return Math.max.apply(null, arguments);
}
答案 1 :(得分:2)
function get_max(num1, num2, num3)
{
var max = Math.max(num1, num2, num3);
return max;
}
alert(get_max(20,3,5)); // 20
答案 2 :(得分:2)
没有必要创建一个已存在的新功能:
Math.max(num1, num2, num3);
创建新函数只是额外的开销而没有添加任何值。
答案 3 :(得分:1)
抓紧抓住它。
function threeNumberSort(a,b,c) {
if (a<b) {
if (a<c) {
if (b<c) {
console.log(a + ", then " + b + ", then " + c);
} else {
console.log (a + ", then " + c + ", then " + b);
}
} else {
console.log (c + ", then " + a + ", then " + b);
}
} else {
if (b<c) {
if (a<c) {
console.log (b + ", then " + a + ", then " + c);
} else {
console.log (b + ", then " + c + ", then " + a);
}
} else {
console.log (c + ", then " + b + ", then " + a);
}
}
}
threeNumberSort(1456,215,12488855);
这将在您的控制台上打印:
215, then 1456, then 12488855
我使用了算法I found on this page。那里可能存在更有效的。
答案 4 :(得分:0)
这是我使用函数创建的手动代码,以及if语句:
function maxOfThree(a, b, c) {
if ((a >= b) && (a >= c)) {
return a;
} else if ((b >= a) && (b >= c)) {
return b;
} else {
return c;
}
}
console.log(maxOfThree(343,35124,42));