'variable1 / variable2 = NaN',其中两个变量都是数字2

时间:2017-02-16 02:03:02

标签: javascript variables division

我不知道该怎么做,在尝试自己解决这个问题的过程中我把它打印出来(在CMD中):

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 个答案:

答案 0 :(得分:0)

错误

  

(testData.topics [Z] .percentageMarks)

" percentageMarks"

为了记录,你也可以写var topic = testData.topics[z]

像你这样的问题只有在你有很长的代码行时才会更容易。

您还可以调整代码以便于阅读。

答案 1 :(得分:-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));