Javascript返回基于日期的平均测试结果数组

时间:2014-10-13 08:21:42

标签: javascript arrays average

说我有这个阵列:

testResults = [
    {
        dateCreated: "2014-07-12",
        score: 27.1
    },
    {
        dateCreated: "2014-05-11",
        score: 99.3,
    },
    {
        dateCreated: "2014-07-22",
        score: 88.8
    },
    {
        dateCreated: "2014-07-01",
        score: 33.3
    }
];

我想创建一个函数:

testResultsstartDateendDateinterval作为参数,计算平均测试结果并返回数组:

calculateTestAverages(testResults, "2014-04-01", "2014-07-30", "month");

将返回

testAverages = [0, 99.3, 0, 49.7]

我想现在我想创建一个中间二维数组:

intermediaryArray = [ [0], [99.3], [0], [27.1, 88.8, 33.3] ]

因为计算这个中间阵列的平均值很容易。

到目前为止我所做的并不起作用:

// takes a string date and converts it into a Date object.
var convertToDateObject = function(date){
    dateObject = new Date(date);
    return dateObject;
}

// takes a string date and returns the month.
var getMonth = function(date){
    return convertToDateObject(date).getMonth();
}

var calculateTestAverages = function(testResults, startDate, endDate, interval){

    var intermediaryArray = [];

    if( interval == "month" ){

        startingMonth = getMonth(startDate);
        endingMonth = getMonth(endDate);
        maxArrayIndex = endingMonth - startingMonth;

        for (var i = 0; i <= maxArrayIndex; i++){
            intermediaryArray[i] = [];
            for (var month = startingMonth; month <= endingMonth; month++){
                for (var testNumber = 0; testNumber < testResults.length; testNumber++){
                    if ( getMonth(testResults[testNumber].dateCreated) == month ){
                        intermediaryArray[i].push(testResults[testNumber].score);
                    };
                };
            };
        };
    };

    return intermediaryArray;

}

我已经被困在这件事上好几个小时了。我觉得我的大脑在这一点上有点油腻。

1 个答案:

答案 0 :(得分:2)

我先回答你的问题然后解释出了什么问题。不用担心,你的代码背后的逻辑绝对没问题。它只是一个错位的大括号,即}

让我们看一下嵌套的for循环,看看它们是如何进行的:

 var intermediaryArray = []; //initialized earlier

 ...

 for (var i = 0; i <= maxArrayIndex; i++){
        intermediaryArray[i] = [];
        for (var month = startingMonth; month <= endingMonth; month++){
            for (var testNumber = 0; testNumber < testResults.length; testNumber++){
                if ( getMonth(testResults[testNumber].dateCreated) == month ){
                    intermediaryArray[i].push(testResults[testNumber].score);
                };
            };
        };
    };

在您提供的测试用例中,intermediaryArray的长度为4,可以完美计算。最外面的for循环运行四次。对于每次迭代,你都要做这个部分:

  for (var month = startingMonth; month <= endingMonth; month++){
        for (var testNumber = 0; testNumber < testResults.length; testNumber++){
            if ( getMonth(testResults[testNumber].dateCreated) == month ){
                intermediaryArray[i].push(testResults[testNumber].score);
            };
        };
    };

上面的代码基本上从startingMonth循环到endingMonth,并在该月找到testResulsts数组的相应元素,并且应该将其推送到intermediaryArray

从本质上讲,特定月份的一个条目应代表index中的intermediaryArray。但是,在{{1>} 索引for的{​​{1}}循环中,您可以针对所有月份进行此检查。让我们对intermediaryArray

进行干预
i = 0

这就是为什么你在最终返回 i = 0 ----------- startingMonth = 4, endingMonth = 7 month = 4 : Nothing gets pushed into intermediaryArray[0] month = 5 : {dateCreated: "05/11/2014",score: 99.3} gets pushed into intermediaryArray[0] month = 6 : Nothing gets pushed month = 7 : The remaining 3 tests get pushed into intermediaryArray[0] Ultimately all the results get pushed into intermediaryArray[0] as we did not change i with the month variable 时,你在所有循环结束时推动了所有intermediaryArray的原因。

解决方法非常简单,您只需初始化testResults一次,然后填充intermediaryArray,即每个月推送新元素。换句话说,您循环遍历intermediaryArray[i]循环中的intermediaryArray循环数月,因为元素之间存在 one-one 对应关系。

这样的东西就足够了:

for

在编码之前规划我们对 for (var i = 0; i <= maxArrayIndex; i++){ intermediaryArray[i] = []; //Initialize the empty array here based on the size }; for (var month = startingMonth,i=0; month <= endingMonth; month++,i++){ //Simply update the value of i when you update the value of month. //You don't have to enclose the whole thing into the loop that initialises intermediaryArray for (var testNumber = 0; testNumber < testResults.length; testNumber++){ if ( getMonth(testResults[testNumber].dateCreated) == month ){ intermediaryArray[i].push(testResults[testNumber].score); }; }; }; 循环强制执行的约束是很好的。一旦了解了遍历数组的方式以及存储数组的方式,就可以将正确的条件插入到for循环中。希望它澄清事情并帮助您开始朝着正确的方向发展。