Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JScript代码赢得';t平均数_Javascript - Fatal编程技术网

Javascript JScript代码赢得';t平均数

Javascript JScript代码赢得';t平均数,javascript,Javascript,我的代码有什么问题?我已经创建了6个文本框。前五个框代表用户想要的任何数字,最后一个框应该显示数字的平均值。我将非常感谢任何帮助 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/DTD/xhtml1.0-transitional.dtd" <html xmlns="http://www.w3.org/1999/xhtml" <head>

我的代码有什么问题?我已经创建了6个文本框。前五个框代表用户想要的任何数字,最后一个框应该显示数字的平均值。我将非常感谢任何帮助

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/DTD/xhtml1.0-transitional.dtd"

<html xmlns="http://www.w3.org/1999/xhtml"
    <head>
        <title>Calc Average</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
        /* <![CDATA[ */
            var numberOne= 0;
            var numberTwo = 0;
            var numberThree = 0;
            var numberFour = 0;
            var numberFive = 0;
            var calcResult = 0;

            function calcAvg() {
                performCalc(numberOne, numberTwo, numberThree, numberFour, numberFive);
                return calcResult;
                calcResult = document.numbers.averageResult.value;
            }

            function performCalc() {
                calcResult = parseInt(numberOne + numberTwo + numberThree + numberFour + numberFive) / 5;
                return calcAvg();
            }

        /* ]]> */
        </script>
    </head>
    <body>
        <form action="" name="numbers">
            <table>
                <tr>
                    <td>Number One</td>
                    <td>
                        <input type="text" 
                            name="numOne" value="0" size="5"
                            onchange="var numberOne=calcAvg(document.numbers.numOne.value);" /></td>
                </tr>
                <tr>            
                    <td>Number Two</td>
                    <td>
                        <input type="text"
                            name="numTwo" value="0" size="5"
                            onchange="var numberTwo=calcAvg(document.numbers.numTwo.value);" /></td>
                </tr>
                <tr>
                    <td>Number Three</td>
                    <td>
                        <input type="text"
                            name="numThree" value="0" size="5"
                            onchange="var numberThree=calcAvg(document.numbers.numThree.value);" /></td>
                </tr>       
                <tr>
                    <td>Number Four</td>
                    <td>
                        <input type="text"
                            name="numFour" value="0" size="5"
                            onchange="var numberThree=calcAvg(document.numbers.numFour.value);" /></td>
                </tr>
                <tr>
                    <td>Number Five</td>
                    <td>
                        <input type="text"
                            name="numFive" value="0" size="5"
                            onchange="var numberFour=calcAvg(document.numbers.numFive.value);" /></td>
                </tr>
                <tr>
                    <td>Average of Five Numbers</td>
                    <td>
                        <input type="text"
                            name="averageResult" value="0" size="5"
                            onchange="calcAvg()" /></td>
                </tr>
            </table>
    </body>
</html>

头号人物
二号
三号
四号
五号
五个数字的平均值
更新:JS Fiddle链接用于工作,使用 单函数且无外部变量:

当您更改任何框中的数字时,将更新平均值 当你打字的时候

注:正如我所怀疑的,我回答这个问题并不是为了做家庭作业 这个问题可能是,但因为它值得展示某种形式 本答案中的处理方法,因为此类技术具有很大的通用性 在许多应用中都很有用。其中一些技术包括:

  • 以请求在表单事件处理程序中使用的方式使用此
  • 迭代表单元素,而不是在已经有名称的表单元素上使用易于出错的多个变量(如
    var numberTwo=document.numbers.numTwo.value;var numbertree=document.numbers.numThree.value;…
    )或冗余的DOM操作相关
    id
    s)
  • …并在执行上述操作时避免JavaScript特性
  • 避免非数字数据
  • 在它发光的地方使用onkeyup
  • 避免潜在的数学陷阱,如被零除
关于您的代码,我最初的答案是在这个答案的最后,因为我强烈建议您完全重写您的函数,使其更简单、更少冗余,并使用DOM的一些功能,例如:

function calcAvg(elem) {
    var n = 0, sum = 0, e = elem.form.elements, k;
    for (k in e) {
        if ((''+e[k].name).substring(0, 3) == 'num') {
             sum += (e[k].value-0) || 0, n++;
        }
    }
    if (n)
        elem.form.averageResult.value = sum/n;
}
要触发计算的每个字段都应该有处理程序
onchange=“calcAvg(this)”
或更好的处理程序,
onkeyup=“calcAvg(this)”
用于在键入时进行实时计算

作为旁注,此代码不需要任何其他变量——只需要每个字段中的事件处理程序(您希望与平均过程关联),以及数字字段的命名格式(以
num
开头,作为其名称的前三个字母)。此函数的作用是将包含触发事件的元素的表单中以
num
开头的所有字段中的所有值相加并求平均值,自动忽略非数值以及其他安全功能,如避免被零除,以及其他一些奇怪的浏览器和版本特定行为,这超出了本文讨论的范围


这可能不是代码的唯一问题(看起来代码的某个页面正在运行或部分运行会有所帮助),但我认为我看到了程序逻辑中的主要漏洞

在每个文本输入字段中,都有如下内容(每个事件处理程序的局部变量在事件结束后完全消失):

但是您之前已经将
numberTwo
等定义为全局变量,而
calcAvg
函数只有在它们是全局变量时才起作用(它不接受任何参数,只对全局定义的变量进行操作)

删除每个事件处理程序中的“var”前缀,然后查看程序是否工作

另外,如果你知道怎么做的话,也可以在你的页面上贴一个JS文件,这样我们就可以更容易地“摆弄”它*

更新:JS Fiddle链接用于工作,使用 单函数且无外部变量:

当您更改任何框中的数字时,将更新平均值 当你打字的时候

注:正如我所怀疑的,我回答这个问题并不是为了做家庭作业 这个问题可能是,但因为它值得展示某种形式 本答案中的处理方法,因为此类技术具有很大的通用性 在许多应用中都很有用。其中一些技术包括:

  • 以请求在表单事件处理程序中使用的方式使用此
  • 迭代表单元素,而不是在已经有名称的表单元素上使用易于出错的多个变量(如
    var numberTwo=document.numbers.numTwo.value;var numbertree=document.numbers.numThree.value;…
    )或冗余的DOM操作相关
    id
    s)
  • …并在执行上述操作时避免JavaScript特性
  • 避免非数字数据
  • 在它发光的地方使用onkeyup
  • 避免潜在的数学陷阱,如被零除
关于您的代码,我最初的答案是在这个答案的最后,因为我强烈建议您完全重写您的函数,使其更简单、更少冗余,并使用DOM的一些功能,例如:

function calcAvg(elem) {
    var n = 0, sum = 0, e = elem.form.elements, k;
    for (k in e) {
        if ((''+e[k].name).substring(0, 3) == 'num') {
             sum += (e[k].value-0) || 0, n++;
        }
    }
    if (n)
        elem.form.averageResult.value = sum/n;
}
要触发计算的每个字段都应该有处理程序
onchange=“calcAvg(this)”
或更好的处理程序,
onkeyup=“calcAvg(this)”
用于在键入时进行实时计算

作为旁注,此代码不需要任何其他变量——只需要每个字段中要关联的事件处理程序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/DTD/xhtml1.0-transitional.dtd"

<html xmlns="http://www.w3.org/1999/xhtml"
    <head>
        <title>Calc Average</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <form action="" name="numbers">
            <table>
                <tr>
                    <td>Number One</td>
                    <td>
                        <input type="text" 
                            name="numOne" value="0" size="5"
                            onchange="calcAvg();" /></td>
                </tr>
                <tr>            
                    <td>Number Two</td>
                    <td>
                        <input type="text"
                            name="numTwo" value="0" size="5"
                            onchange="calcAvg();" /></td>
                </tr>
                <tr>
                    <td>Number Three</td>
                    <td>
                        <input type="text"
                            name="numThree" value="0" size="5"
                            onchange="calcAvg();" /></td>
                </tr>       
                <tr>
                    <td>Number Four</td>
                    <td>
                        <input type="text"
                            name="numFour" value="0" size="5"
                            onchange="calcAvg();" /></td>
                </tr>
                <tr>
                    <td>Number Five</td>
                    <td>
                        <input type="text"
                            name="numFive" value="0" size="5"
                            onchange="calcAvg();" /></td>
                </tr>
                <tr>
                    <td>Average of Five Numbers</td>
                    <td>
                        <input type="text" name="averageResult" value="0" size="5" /></td>
                </tr>
            </table>
    </body>
            <script type="text/javascript">
        /* <![CDATA[ */

            function calcAvg() {
              var numberOne= document.numbers.numOne.value;
              var numberTwo = document.numbers.numTwo.value;
              var numberThree = document.numbers.numThree.value;
              var numberFour = document.numbers.numFour.value;
              var numberFive = document.numbers.numFive.value;
              var calcResult = 0;
                calcResult = (parseInt(numberOne) + parseInt(numberTwo) + parseInt(numberThree) + parseInt(numberFour) + parseInt(numberFive)) / 5;
                document.numbers.averageResult.value = calcResult;
            }

        /* ]]> */
        </script>

</html>
            function calcAvg() {
                performCalc(numberOne, numberTwo, numberThree, numberFour, numberFive);
                return calcResult;
                calcResult = document.numbers.averageResult.value;
            }

            function performCalc(parameters) {
                calcResult = parseInt(Number(numberOne) + Number(numberTwo) + Number(numberThree) + Number(numberFour) + Number(numberFive)) / 5;
                return calcResult();
            }
<html xmlns="http://www.w3.org/1999/xhtml"
    <head>
        <title>Calc Average</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript">
        /* <![CDATA[ */

            function performCalc() {

                var sum=0;
                var ele = document.getElementsByTagName('input');
                for(var i=0;i<ele.length;i++)
                { 
                   if(ele[i].id !='output')
                   sum+= parseInt(ele[i].value);
                }

                document.getElementById('output').value=(sum/(ele.length-1));
            }

        /* ]]> */
        </script>
    </head>
    <body>
        <form action="" name="numbers">
            <table>
                <tr>
                    <td>Number One</td>
                    <td>
                        <input type="text" name="numOne" value="0" size="5" onblur="performCalc(this)"/></td>
                </tr>
                <tr>            
                    <td>Number Two</td>
                    <td>
                        <input type="text" name="numTwo" value="0" size="5" onblur="performCalc(this)"/></td>

                </tr>
                <tr>
                    <td>Number Three</td>
                    <td>
                        <input type="text" name="numThree" value="0" size="5" onblur="performCalc(this)"/></td>

                </tr>       
                <tr>
                    <td>Number Four</td>
                    <td>
                        <input type="text" name="numFour" value="0" size="5" onblur="performCalc(this)"/></td>

                </tr>
                <tr>
                    <td>Number Five</td>
                    <td>
                        <input type="text" name="numFive" value="0" size="5" onblur="performCalc(this)"/></td>

                </tr>
                <tr>
                    <td>Average of Five Numbers</td>
                    <td>
                        <input type="text" id="output" name="averageResult" value="0" size="5" onfocus="this.blur();"/></td>
                </tr>
            </table>
    </body>
</html>
        function calcAvg() {
          var calcResult = 0;
          someArray = [
             document.numbers.numOne.value,
             document.numbers.numTwo.value,
             document.numbers.numThree.value,
             document.numbers.numFour.value,
             document.numbers.numFive.value
          ]
            calcResult = SomeArray.reduce(function(x,y){return x+y;});
            document.numbers.averageResult.value = calcResult;
       }