JavaScript中的标准偏差计算器

时间:2015-10-23 16:17:58

标签: javascript arrays standard-deviation

我制作了一个标准偏差计算器,但不确定我在计算中出错了。我正在努力学习如何操作数组,所以一切都可能需要很长的路径来完成我的工作。

我做错了什么?

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.

2 个答案:

答案 0 :(得分:1)

您已嵌套了两个点击事件:

$('#showArray').on('click', function() {
    //
    $('#doMath').on('click', function() {
        //
    });
});

您应该将#doMath上的click事件单独移动,以便在您实际单击该元素时可以运行它。

答案 1 :(得分:1)

这是一个非常简单的错误,你正在做

 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]
 };