JavaScript中五个输入中最小和最大的

时间:2017-02-02 15:55:20

标签: javascript

您好我正在上课,尝试解决这个问题,获取五个输入并给出最小和最大的输入。我写了一些代码,但它不起作用,我不明白为什么。任何帮助都会很棒。有人能指出我错误的方向。我可以在输入中写入,但是当它给出一个最大和最小的输出时显示为NaN

这是代码。

<html>
    <head>
        <meta charset = "utf-8">
        <title> 6.20 Homework </title>
        <script type = "text/javascript">

        var i1, i2, i3, i4, i5; //input numbers
        var largest; 
        var smallest; //largest and smallest

        i1=window.prompt ("Please enter first integer","0");
        i2=window.prompt ("Please enter second integer","0");
        i3=window.prompt ("Please enter third integer","0");
        i4=window.prompt ("Please enter fourth integer","0");
        i5=window.prompt ("Please enter fifth integer","0");

        i1 = largest;
        i1 = smallest;


        if ( i2 > largest )
            { i2 = largest}

        if ( i3 > largest )
            { i3 = largest}

        if ( i4 > largest )
            {i4 = largest} 

        if ( i5 > largest )
            { i5 = largest}



        if (i2 < smallest )
            {i2 = smallest}

        if (i3 < smallest )
            {i3 = smallest}

        if (i4 < smallest )
            {i4 = smallest}

        if (i5 < smallest )
            {i5 = smallest}

        document.writeln(+largest+ " is the largest");
        document.writeln(+smallest+ " is the smallest");
        </script>
    </head>
</html>

5 个答案:

答案 0 :(得分:2)

您可以使用Math.max()Math.min()代替if语句:

var largest = Math.max(i1, i2, i3, i4, i5);
var smallest = Math.min(i1, i2, i3, i4, i5);

答案 1 :(得分:1)

将所有=操作更改为相反的操作,如下所示:smallest = i1;

答案 2 :(得分:1)

尝试这样做。使用Math.maxMath.min分别找到最大和最小的数字。

        var i1, i2, i3, i4, i5; //input numbers
        var largest; 
        var smallest; //largest and smallest

        i1=window.prompt ("Please enter first integer","0");
        i2=window.prompt ("Please enter second integer","0");
        i3=window.prompt ("Please enter third integer","0");
        i4=window.prompt ("Please enter fourth integer","0");
        i5=window.prompt ("Please enter fifth integer","0");
       
       largest =  Math.max(i1,i2,i3,i4,i5);
       smallest = Math.min(i1,i2,i3,i4,i5);
       console.log(largest);
       console.log(smallest);

答案 3 :(得分:0)

在每一行看起来像这样:i1 = largest;您要更改整数的值,而不是largestsmallest的值,您应该在那里切换顺序:{{1 }}

答案 4 :(得分:-1)

我可能会建议你先创建一个数组和一个插入函数,将输入插入到数组中。通过这样做,每次获得输入时,您都可以在数组的正确位置插入数字。喜欢:

var insert = function(arr,inp){
  if(arr.length == 0) { arr.push(inp); return; }
  var inserted = false;
  for(var i=0;i<arr.length;i++){
    if(arr[i] > inp){
     var temp = arr[i];
     arr[i] = inp;
     insert(arr,temp);
     return;
    }
  }
  arr.push(inp);
}

var inputs;                //array to insert inputs
var i1, i2, i3, i4, i5; //input numbers

i1=window.prompt ("Please enter first integer","0");
insert(inputs,i1);
i2=window.prompt ("Please enter second integer","0");
insert(inputs,i2);
i3=window.prompt ("Please enter third integer","0");
insert(inputs,i3);
i4=window.prompt ("Please enter fourth integer","0");
insert(inputs,i4);
i5=window.prompt ("Please enter fifth integer","0");
insert(inputs,i5);

document.writeln(+inputs[inputs.length-1]+ " is the largest");
document.writeln(+inputs[0]+ " is the smallest");