简体   繁体   English

此JavaScript代码有什么问题?

[英]Whats wrong with this javascript code?

<!doctype html>
<html>
<head>
    <title>Largest number</title>
</head>
<body>
    <center>
        <h2>largest number</h2>
        <script language="Javascript" type="text/Javascript">

            function Max(num1,num2,num3)
            {
               var largest=arguments[num1,num2,num3]
               for(i=0;i<arguments.length;i++)
               {
                  if((num1>num2)&&(num1>num3))
                      largest=num1
                  else if((num2>num1)&&(num2>num3))
                      largest=num2
                  else
                      largest=num3
               }
               return(largest)
            }
            document.write("</br>")
            var num1 = prompt("Enter first number"," ")
            var num2 = prompt("Enter second number"," ")
            var num3 = prompt("Enter third number"," ")
            var large = Max(num1,num2,num3)
            document.write("You entered",num1,",",num2,",",num3)
            document.write("</br>")
            document.write("The largest number is :",large)
        </script>
    </center>
</body>
</html>

This program accepts 3 numbers through prompt. 该程序通过提示符接受3个数字。 Only for specific numbers it gives strange and unexpected output. 仅对于特定的数字,它会提供奇怪和意外的输出。 If I give input for this program as 5,21 and 100 when each prompt appears, the output will be given 5 as a largest number. 如果在每个提示出现时我给该程序输入的数字分别为5,21和100,则输出将被赋予5作为最大数字。 Even for the input 10,24 and 5, the output will be 5. 即使对于输入10,24和5,输出也将为5。

Is there any problem while using if condition or array. 使用if条件或数组时是否有任何问题。

Please help me. 请帮我。

You're not actually passing numbers to the function. 您实际上并没有将数字传递给该函数。 prompt() values are stored as strings. prompt()值存储为字符串。

Lexicographically, "5" is greater than "100" since "5" comes after "1". 从词法上讲,“ 5”大于“ 100”,因为“ 5”在“ 1”之后。

You need to use parseInt() to ensure integers are passed in, or if you allow decimal values, then parseFloat() . 您需要使用parseInt()来确保传入整数,或者如果允许十进制值,则可以使用parseFloat()

var large = Max(parseInt(num1, 10), parseInt(num2, 10), parseInt(num3, 10));

See jsFiddle jsFiddle

You may wish to do the number parsing within the Max() function to ensure that the arguments are always integers. 您可能希望在Max()函数中进行数字解析,以确保参数始终为整数。

Also, the initial assignment of arguments[num1, num2, num3] to largest doesn't make sense. 同样,将arguments[num1, num2, num3]的初始赋值给largest也没有意义。 The variable just needs to be declared. 该变量只需要声明。 You also have an unnecessary loop in your function. 您的函数中也有不必要的循环。

Another example related to this could be 与此相关的另一个示例可能是

var myArray = [45,50,2,99,0];
var result = Math.max.apply(Math,myArray);
document.write("Max value is = "+result );

Hope its solve your confusion. 希望它能解决您的困惑。

There are a few issues with your code. 您的代码存在一些问题。 First of all, you shouldn't try to access multiple values inside the arguments , especially not by values instead of indexes. 首先,您不应该尝试访问arguments内部的多个值,尤其是不要通过值而不是索引来访问。

var largest;

should be enough in your case. 在您的情况下应该足够了。 Next, you are running a for loop multiple times for absolutely no purpose, since you're checking the condition already. 接下来,由于已经在检查条件,因此您绝对没有目的多次运行for循环。 Decide if you want to use a for look or chained else/if statements. 确定是否要使用for look或链接的else / if语句。 Also, by looking at your code, it looks like you want to make this function accept as many parameters as you want, and this implementation of yours does not support that. 另外,通过查看您的代码,您似乎希望使此函数接受所需的尽可能多的参数,而您的此实现不支持该参数。 So, here is a fixed version of the code that should work with just about any number of parameters (more than 0). 因此,这是该代码的固定版本,几乎可以使用任何数量的参数(大于0)。

function Max(){
    var largest;
    for(i=0;i<arguments.length;i++){
        var intval = parseInt(arguments[i]);
        if (intval > largest)
            largest = intval;
    }
    return largest;
}

You can use method like that: 您可以使用如下方法:

var max = function(){
    var p = [];
    for(var i = 0; i < arguments.length; i++)
        p.push(parseInt(arguments[i]) || null);
    return Math.max.apply(Math.max, p);
}

console.log(max(1, 2, "123" ,"4", "null", "123123123"))

You can play with demo 你可以玩演示

Advanced example 进阶范例

var max = function(){
    var p = [];
    for(var i = 0; i < arguments.length; i++){
        p.push(parseInt(arguments[i]) || null);
    }
    return Math.max.apply(Math.max, p);
}

var number = prompt("Enter the number of input");
if(parseInt(number) != NaN){
    var l = [];
    for(var i = 0; i < parseInt(number); i++){
        var e = prompt("Enter number #" + (i+1));
        l.push(e);
    }
    alert("Largest number is " + max.apply(null, l));
}

Play with advanced demo 播放高级演示

You need to use parseInt to ensure that the numbers are treated as numbers and then the ordering is as you expect. 您需要使用parseInt来确保将数字视为数字,然后顺序是您期望的。

function Max(num1,num2,num3)
{
 var largest=arguments[num1,num2,num3]
 for(i=0;i<arguments.length;i++)
 {
  var i1 = parseInt(num1);
  var i2 = parseInt(num2);
  var i3 = parseInt(num3);
  if((i1>i2)&&(i1>i3))
  largest=i1
  else if((i2>i1)&&(i2>i3))
  largest=i2
  else
  largest=i3
 }
 return(largest)
}
document.write("</br>")
var num1 = prompt("Enter first number"," ")
var num2 = prompt("Enter second number"," ")
var num3 = prompt("Enter third number"," ")
var large = Max(num1,num2,num3)
document.write("You entered",num1,",",num2,",",num3)
document.write("</br>")
document.write("The largest number is :",large)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM