设置对象值时出错[JavaScript]

时间:2015-11-03 12:40:33

标签: javascript jquery object javascript-objects standard-deviation

我构建了一个标准偏差计算器,它使用对象按预期工作。但是,现在我想将每个对象中newScore的属性设置为对象z得分((得分 - 均值)/标准差)。

但是,似乎每个对象都添加了相同的z分数,我不知道为什么。

我做错了什么?

Fiddle here.

HTML:

<input type="text" id="name" placeholder="Name">
<input type="text" id="score" placeholder="Score">
<br>
<br>
<button id="push">Push to Array</button>
<button id="show">Show</button>
<button id="doMath">Do Math</button>
<ul id="list"></ul>
<p id="sum"></p>
<p id="mean"></p>
<p id="variance"></p>
<p id="std"></p>

CSS:

input:hover {
    border: 1px solid black;
}

JavaScript:

var myArray = [];
var sumOfScores;
var mean;
var variance;
var secondArray = [];
var thirdArray = [];
var squaredSum;
var std;
$(document).ready(function () {
    $('#push').on('click', function () {
        var name = $('#name').val();
        var score = parseInt($('#score').val());
        myArray.push((student = {
            name: name,
            score: score,
            newScore: null
        }));
        console.log(student);
        $('#name').val("");
        $('#score').val("");
    });
    $('#show').on('click', function () {
        $('#list').html("");
        for (i = 0; i < myArray.length; i++) {
            $('#list').append("<li>" + myArray[i].name + " received a score of " + myArray[i].score + "</li>");
        };
    });
    $('#doMath').on('click', function () {
        sumOfScores = 0;
        for (i = 0; i < myArray.length; i++) {
            sumOfScores += myArray[i].score;
        };
        $('#sum').html("The sum is: " + sumOfScores);
        mean = (sumOfScores / myArray.length);
        $('#mean').html("The mean score is: " + mean);
        variance = 0;
        for (i = 0; i < myArray.length; i++) {
            console.log(myArray[i].score);
            secondArray.push(myArray[i].score - mean);
        };
        console.log(mean);
        console.log("The second array is: " + secondArray);
        for (i = 0; i < secondArray.length; i++) {
            thirdArray.push(Math.pow(secondArray[i], 2));
        };
        console.log("The third array is: " + thirdArray);
        squaredSum = 0;
        for (i = 0; i < thirdArray.length; i++) {
            squaredSum += thirdArray[i];
        };
        console.log("The squared sum is: " + squaredSum);
        variance = 0;
        variance = (squaredSum / ((thirdArray.length) - 1));
        console.log("The variance is: " + variance);
        $('#variance').html("The variance is: " + variance);
        std = Math.sqrt(variance);
        $('#std').html("The standard deviation is: " + std);
        console.log("The standard deviation is: " + std);
        //figure out z score
        for (i = 0; i < myArray.length; i++) {
            myArray[i].newScore = ((myArray[i].newScore - mean) / std);
        };
        for (i = 0; i < myArray.length; i++) {
            console.log(myArray[i].newScore);
        };
    });
});

1 个答案:

答案 0 :(得分:0)

Per Abhitalks,第61行应阅读:

myArray[i].newScore = ((myArray[i].score - mean) / std);

然而,我有:

myArray[i].newScore = ((myArray[i].newScore - mean) / std);

这不起作用,因为newScore不是该计算的值,并且设置为null,您无法乘以。