为什么这个函数返回NaN?

时间:2013-09-11 15:27:57

标签: javascript

function getNucleobaseCount (strand) {
  /*
  Returns occurences of nucleobases A and C respectively
  */
  var countA = (strand.split("A").lenght - 1);
  var countC = (strand.split("C").lenght - 1);
  return countA + " " + countC;
}

但是

> console.log(getNucleobaseCount("AAGCATT"))
Nan Nan

而不是预期的3 1

为什么?

3 个答案:

答案 0 :(得分:3)

lenght属性的值为undefined

undefined - 1NaN

你拼错了length

答案 1 :(得分:1)

length的拼写错误?我认为它会显示编译错误。

答案 2 :(得分:-2)

你有 拼写错误的'长度': - )

function getNucleobaseCount (strand) {
  /*
  Returns occurences of nucleobases A and C respectively
  */
  var countA = (strand.split("A").length - 1);
  var countC = (strand.split("C").length - 1);
  return countA + " " + countC;
}