2017-02-16 83 views
1

我在這裏試圖弄清楚這個過程中我不知道該怎麼做,我打印出來了(CMD):'變量1 /變量2 = NaN',其中兩個變量都是數字2

testData.topics[z].percentageMark :2 
testData.topics[z].questions.length :2 
typeof(testData.topics[z].percentageMark) :number 
typeof (testData.topics[z].questions.length) :number 
FINAL : testData.topics[z].percentageMark :NaN 

這是代碼的結果(對大對象不好意思):

console.log("testData.topics[z].percentageMark :" + testData.topics[z].percentageMark); 
console.log("testData.topics[z].questions.length :" + testData.topics[z].questions.length); 
console.log("typeof(testData.topics[z].percentageMark) :" + typeof (testData.topics[z].percentageMark)); 
console.log("typeof (testData.topics[z].questions.length) :" + typeof (testData.topics[z].questions.length)); 
testData.topics[z].percentageMark = ((testData.topics[z].percentageMarks)/(testData.topics[z].questions.length)); 
console.log("FINAL : testData.topics[z].percentageMark :" + testData.topics[z].percentageMark); 

我真正感到困惑,在這裏做什麼,我看不出這裏簡單的劃分是行不通的。

+2

第5行代碼...'percentageMarks'!=='percentageMark' –

回答

0

這裏錯字

(testData.topics [Z] .percentageMarks)

「percentageMarks」

根據記錄,你可能也寫var topic = testData.topics[z]

問題像你的只有當你有很長的代碼行時更容易。

您還可以對齊您的代碼以便於閱讀。

-1

您有輸入錯誤,percentageMarks應該是percentageMarktestData.topics[z].percentageMarksundefined並且當您將undefined除以數字時,您會得到NaN

所以,從

testData.topics[z].percentageMark = ((testData.topics[z].percentageMarks)/(testData.topics[z].questions.length)); 

代碼更改爲

testData.topics[z].percentageMark = ((testData.topics[z].percentageMark)/(testData.topics[z].questions.length)); 
+0

任何對downvote原因是什麼? – Agalo