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

'variable1 / variable2 = NaN' where both variables are the number 2

我在这里不知所措,在尝试自己解决这个问题的过程中,我打印了这个(在 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);

我真的很困惑这里要做什么,我看不出这里简单的除法是行不通的。

此处打字错误

(testData.topics[z].percentageMarks)

"percentageMarks"

为了记录,你不妨写var topic = testData.topics[z]

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

您还可以对齐代码以便于阅读。

您有错别字,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));