Javascript函数代码问题

时间:2018-03-10 15:29:01

标签: javascript

我想知道我是否能得到人们的意见。我正在做一个基本编码的javascript类。我们刚刚开始讨论函数,但我无法返回结果。

我制作了两个节目:

3.1应该从提示中获取3个数字并返回最小和最大的数字:

<html>
    <head>
        <meta charset = "utf-8">
        <title>Even to Odd</title>
        <script>
 // Program name: Assignment 3.1
// Purpose: Finds smallest and largest numbers
// Author: Eric Messihi
// Date last modified: 09-Mar-2018
        function oddEven(num){
         if(num%2==0)
                {
                document.write('the number is Even.');
                }
            else
                {
                document.write('the number is Odd.');    
                }   
        }
        </script>
    </head>
    <body>
        <script type = "text/javascript">
        //variables
            var num;
            
        //prompts for integers
            num = parseInt(prompt('Hi! Pick a number, any number', ""));
            
            // Call the module, display the result
            
        
        </script>
</body>
</html>

3.2应该采用给定的数字,并指出数字是偶数还是奇数:

<html>
    <head>
        <meta charset = "utf-8">
        <title>Smallest to Largest</title>
        <script>
// Program name: Assignment 3.1
// Purpose: Finds smallest and largest numbers
// Author: Eric Messihi
// Date last modified: 09-Mar-2018
        function min(n1, n2, n3){
            //declare variable
            var smallest;
            smallest = n1
   	  	if (n2 < smallest) {
   	  	    smallest = n2;
   	  	}
   	  	if (n3 < smallest) {
   	  	    smallest = n3;
   	  	}
            return min;
            }
        function max(n1, n2, n3){
            //declare variable
            var largest;
            largest = n1
   	  	if (n2 > largest) {
   	  		largest = n3;
   	  	}
   	  	if (n3 > largest) {
   	  		largest = n3;
   	  	}
            return max;
        </script>
    </head>
    <body>
        <script type = "text/javascript">
//variables
            var n1, n2, n3;
            var min = "";
            var max = "";
//prompts for integers
n1 = parseInt(prompt('Hi! What is your first number?', ""));
 n2 = parseInt(prompt('Great! What is your second number?', ""));
 n3 = parseInt(prompt('Almost done! What is your third number?', ""));
   
        // Call the four modules, display the results
    min = smallest;
   document.write("The smallest of the three numbers is: " + min);
    max = largest;
   document.write("The largest of the three numbers is: " + max);

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
<html>
    <head>
        <meta charset = "utf-8">
        <title>Even to Odd</title>
        <script>
 // Program name: Assignment 3.1
// Purpose: Finds smallest and largest numbers
// Author: Eric Messihi
// Date last modified: 09-Mar-2018
        function oddEven(num){
         if(num%2==0)
                {
                document.write('the number is Even.');
                }
            else
                {
                document.write('the number is Odd.');    
                }   
        }
        </script>
    </head>
    <body>
        <script type = "text/javascript">
        //variables
            var num;
            
        //prompts for integers
            num = parseInt(prompt('Hi! Pick a number, any number', ""));
             oddEven(num)
            // Call the module, display the result
            
        
        </script>
</body>
</html>
&#13;
&#13;
&#13;

&#13;
&#13;
<html>

<head>
  <meta charset="utf-8">
  <title>Smallest to Largest</title>
  <script>
    // Program name: Assignment 3.1
    // Purpose: Finds smallest and largest numbers
    // Author: Eric Messihi
    // Date last modified: 09-Mar-2018
    function getmin(n1, n2, n3) {
      //declare variable
      var smallest;
      smallest = n1
      if (n2 < smallest) {
        smallest = n2;
      }
      if (n3 < smallest) {
        smallest = n3;
      }
      
      return smallest;
    }

    function getmax(a, b, c) {
      //declare variable
      var largest;
      
      if (a>b  && a>c)
      {
      return a;
      }

      if (b>a  && b>c)
      {
        return b;
      }

      else
      {
        return c;
      }
      
      
      return largest;
    }
  </script>
</head>

<body>
  <script type="text/javascript">
    //variables
    var n1, n2, n3;
    var min = "";
    var max = "";
    //prompts for integers
    n1 = parseInt(prompt('Hi! What is your first number?', ""));
    n2 = parseInt(prompt('Great! What is your second number?', ""));
    n3 = parseInt(prompt('Almost done! What is your third number?', ""));

    // Call the four modules, display the results
    min = getmin(+n1, +n2, +n3);
    document.write("The smallest of the three numbers is: " + min);
    max = getmax(n1, n2, n3);
    document.write("The largest of the three numbers is: " + max);
  </script>
</body>

</html>
&#13;
&#13;
&#13;

您的代码存在的问题是您使用局部变量覆盖了min和max的函数定义。因此,当您尝试调用该函数时,它说min / max不是函数(因为本地最小/最大变量会覆盖函数行为。

尝试更改功能名称及其工作原理。