2011-08-03 23 views
0
Player.prototype.d2 = function(ratingList, rdList) { 
    var tempSum = 0; 
    for (var i = 0; i < ratingList.length; i++) { 
     var tempE = this.e(ratingList[i], rdList[i]); 
     tempSum += Math.pow(this.g(rdList[1]), 2) * tempE * (1 - tempE); 
    } 
    return 1/Math.pow(q, 2) * tempSum; 
}; 

這似乎是有點問題。如果數組長度爲1,則陣列上的操作返回NaN

一切似乎都很好,除非ratingList,rdListoutcomeList只包含一個值。然後東西被設置爲NaN。我試着改變指數爲-1,改變比較ratingList.length - 1,即使遞減for循環試過,但它似乎總是返回NaN的,如果陣列中只包含一個值。

有沒有什麼辦法(我敢肯定存在 - 我想問題是如何)取消for循環,並用Array.map()或zip或其它任何組合的函數替換它?

You can see ALL of the code here -- it's about 60 LOC

+0

並以 「只包含一個值」 你的意思是:'ratingList = 「OneValue」]'或'ratingList = 「OneValue」'? – Cipi

+0

ratingList = [1400] –

回答

2

d2功能,您有這條線在for循環:

tempSum += Math.pow(this.g(rdList[1]), 2) * tempE * (1 - tempE); 

所以假定rdList至少爲2元,但你只有一個bob

也許它必須rdList[i]

+0

是的,就是這樣。可能想rdList [i] –

+0

感謝!另一方面,現在我感到很蠢,哈哈。如果有人知道如何用其他結構替換循環,仍然學術上好奇。 –