我做了一个标准偏差计算器,但不确定在哪里出错。我正在尝试学习如何操作数组,因此一切都可能在我做的事情上走很长的路。

我究竟做错了什么?

JavaScript:

myArray = [];
    squaredArray = [];
    var theTotal;
    var average;
    var theSquaredTotal;
    var meanOfSquaredArray;
    var squaredAverage;
    var standardDeviation;
    $(document).ready(function() {
        $('#inTakeButton').on('click', function() {
            var inputValue = parseFloat($('#input').val());
            if (inputValue === "") {
                return;
            }
            if (isNaN(inputValue)) {
                $('#input').val("");
                return;
            }
            myArray.push(inputValue);
            $('#input').val("");
        });
        $('#showArray').on('click', function() {
            console.log(myArray);
            $('#list').html("");
            for (var i = 0; i < myArray.length; i++) {
                $('#list').append("<li>" + myArray[i] + "</ul>");
            };
            $('#doMath').on('click', function() {
                theTotal = 0;
                for (var i = 0; i < myArray.length; i++) {
                    theTotal = theTotal + myArray[i];
                };
                $('#sum').html("");
                $('#sum').html("The sum is: " +
                    theTotal);
                average = parseFloat((theTotal /
                    myArray.length));
                $('#average').html("");
                $('#average').html(
                    "The mean value is: " + average
                );
                for (var i = 0; i < myArray.length; i++) {
                    squaredArray.push(myArray[i] -
                        average);
                };
                console.log(
                    "the subtracted squared away array is: " +
                    squaredArray);
                for (var i = 0; i < myArray.length; i++) {
                    squaredArray[i] = Math.pow(
                        squaredArray[i], 2);
                };
                console.log(
                    "the squared away array is: " +
                    squaredArray);
                for (var i = 0; i < squaredArray.length; i++) {
                    squaredTotal = 0;
                    squaredTotal = squaredTotal +
                        squaredArray[i]
                };
                console.log("The squared sum is: " +
                    squaredTotal);
                //meanOfSquaredArray =
                squaredAverage = parseFloat((
                    squaredTotal / squaredArray
                    .length));
                console.log("The squared average is: " +
                    squaredAverage);
                standardDeviation = Math.sqrt(
                    squaredAverage);
                console.log(
                    "The standard deviation is: " +
                    standardDeviation);
            });
        });
    });


CSS:

#wrapper {
    width: 80%;
    margin: 0 auto;
    border: 1px solid #000;
    border-radius: 5px 5px 5px 5px;
    box-shadow: 5px 5px 10px 3px #000;
    padding: 10px;
}

h1 {
    text-align: center;
    color: orange;
    text-shadow: 1px 1px blue;
}

#intake {
    text-align: center;
}


HTML:

    <div id="wrapper">
        <div id="content">
            <h1>Standard Deviation</h1>
            <div id="intake">
                <input id="input" type="text"> <button id="inTakeButton">Push
                to Array</button> <button id="showArray">Show Array</button>
                <button id="doMath">Do Math</button>
            </div>
            <div id="middle">
                <ul id="list"></ul>
                <ul id="sortedList"></ul>
            </div>
            <div id="output">
                <p id="sorted"></p>
                <p id="sum"></p>
                <p id="average"></p>
            </div>
        </div>
    </div>


Fiddle here.

最佳答案

这是一个非常简单的错误,您正在执行

 for (var i = 0; i < squaredArray.length; i++) {
    squaredTotal = 0;
    squaredTotal = squaredTotal + squaredArray[i]
 };


因此,在每一步中,您都将squaredTotal重置为0,这意味着当循环结束时,squaredTotal将等于数组的最后一个值。解决方法是将其置于循环之外:

 squaredTotal = 0;
 for (var i = 0; i < squaredArray.length; i++) {
    squaredTotal = squaredTotal + squaredArray[i]
 };

10-07 22:52