【问题标题】:Error setting object value [JavaScript]设置对象值时出错 [JavaScript]
【发布时间】:2015-11-03 12:40:33
【问题描述】:

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

【问题讨论】:

  • 您可能需要(myArray[i].score - mean) 而不是(myArray[i].newScore - mean)。您的newScore 默认设置为null,因此从null 中减去mean.....
  • 就是这样,谢谢!

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


【解决方案1】:

根据 Abhitalks,第 61 行应该是:

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

然而,我有:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2018-03-19
    • 2011-09-20
    • 2015-06-08
    • 2019-03-24
    相关资源
    最近更新 更多