JavaScript的用户输入的最高20个电话号码 [英] javascript user input highest of 20 numbers
问题描述
这个程序,我需要用户输入20个数字和最高的显示。我做了一个类似与4,我将在下面显示,但有一定是经历这一切更简单的方法?任何意见将是巨大的。
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<脚本类型=文/ JavaScript的>功能MAXNUM(NUM1,NUM2,NUM3,num4){
变种最大= 0;
如果((NUM1&GT = NUM2)及及(NUM1&GT = NUM3)){
最大= NUM1;
}
否则如果((NUM2&GT = NUM1)及及(NUM2&GT = NUM3)及及(NUM3&GT = num4)){
最大= NUM2;
} 否则如果((NUM3&GT = NUM1)及及(NUM3&GT = NUM2)及及(NUM3&GT = num4)){
最大= NUM3;
} 其他{
最大= num4;
}
返回最大值;
} VAR ARR = [];
对于(I = 0; I&下; 4;我++){
改编[I] = parseInt函数(提示(请输入一个数字));
}
的document.write(maxNum.apply(这一点,ARR));< / SCRIPT>
< /身体GT;
< / HTML>
函数findMaxNum(ARR){
返回Math.max.apply(NULL,ARR);
}VAR ARR = [];为(变量I = 0; I&下; 4;我++){
改编[I] = parseInt函数(提示符('输入号码'),10);
}VAR MAXNUM = findMaxNum(ARR);
(注意,添加的基数 for this program, i need the user to enter 20 numbers and for the highest to be displayed. i have done a similar one with 4, which i will display below, however there must be an easier way that going through it all? any advice would be great.
(Note, added the radix for 这篇关于JavaScript的用户输入的最高20个电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Math.max(1,4,10)// 10 $ C:
Math.max
通常用一系列的数字作品$ C>,但在这里我们使用适用
的功能,因此,它接受一个数组来代替。 parseInt函数
)<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function maxNum(num1, num2, num3, num4){
var max = 0;
if((num1 >= num2) && (num1 >= num3)){
max = num1;
}
else if((num2 >= num1) && (num2 >= num3) && (num3 >= num4)){
max = num2;
}
else if((num3 >= num1) && (num3 >= num2) && (num3 >= num4)){
max = num3;
}
else{
max = num4;
}
return max;
}
var arr = [];
for(i=0;i<4;i++){
arr[i] = parseInt(prompt("Enter a number"));
}
document.write(maxNum.apply(this, arr));
</script>
</body>
</html>
function findMaxNum(arr) {
return Math.max.apply(null, arr);
}
var arr = [];
for (var i = 0; i < 4; i++) {
arr[i] = parseInt(prompt('Enter a number'), 10);
}
var maxNum = findMaxNum(arr);
Math.max
usually works with a series of numbers: Math.max(1, 4, 10) // 10
, but here we use apply
on the function so that it accepts an array instead.parseInt
)