设置对象值时出错 [JavaScript]

Error setting object value [JavaScript]

我构建了一个标准偏差计算器,它使用对象按预期工作。但是,现在我想将每个对象中 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);
        };
    });
});

根据 Abhitalks,第 61 行应该是:

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

然而,我有:

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

这是行不通的,因为 newScore 不是该计算的值并且被设置为 null,您不能乘以它。