JavaScript如何求两个数中最大的值
答案:1 悬赏:10
解决时间 2021-01-07 11:24
- 提问者网友:謫仙
- 2021-01-06 18:12
JavaScript如何求两个数中最大的值
最佳答案
- 二级知识专家网友:长青诗
- 2021-01-06 18:32
函数....
function max(a, b) {
return a > b ? a : b;
}
console.log(max(3, 5));追问如果是三个数是怎么算的呢追答function thrmax(a, b, c) {
var max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}还有4个数的?
下面是任意数量的。
function sort(elements){
for(var i=0;i for(var j=0;j if(elements[j]>elements[j+1]){
var swap=elements[j];
elements[j]=elements[j+1];
elements[j+1]=swap;
}
}
}
}
function max() {
sort(arguments);
return arguments[arguments.length - 1];
}
console.log(max(2, 3, 4, 5));
console.log(max(3, 4, 2));
console.log(max(3, 2, 9));
console.log(max(3, 4, 2, 333, 2, 0));
function max(a, b) {
return a > b ? a : b;
}
console.log(max(3, 5));追问如果是三个数是怎么算的呢追答function thrmax(a, b, c) {
var max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}还有4个数的?
下面是任意数量的。
function sort(elements){
for(var i=0;i
var swap=elements[j];
elements[j]=elements[j+1];
elements[j+1]=swap;
}
}
}
}
function max() {
sort(arguments);
return arguments[arguments.length - 1];
}
console.log(max(2, 3, 4, 5));
console.log(max(3, 4, 2));
console.log(max(3, 2, 9));
console.log(max(3, 4, 2, 333, 2, 0));
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯