2017-02-16 61 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