简体   繁体   English

谁能解释为什么我在Javascript中遇到此错误? [无法读取未定义的属性'length']

[英]Can anyone explain why I am getting this error in Javascript? [Cannot read property 'length' of undefined]

Here is my code: 这是我的代码:

function isEven(num) {;
  if (num % 2 === 0) {
  return true;}
  else {
  return false;}
  }

function convertToIntegers(lst) {
  lst.split(' ');
  var len = lst.length;
  var count = 0;
  while (count < len) {
      lst[count] = parseInt(lst[count]);
      count++}
      }




function iqTest(numbers){

  var int_list = convertToIntegers(numbers);
  var e_list = [];
  var o_list = [];
  var count = 0;
  var len_int = int_list.length;
  while (count < len_int) {
      if (isEven(int_list[count]) === true) {
      e_list.push(count);}
      else if (isEven(count) === false) {
      o_list.push(count);}
      count++}
  if (e_list.length < 2 && e_list.length > 0 && o_list.length > 2) {
      return e_list[0];}
  else if (o_list.length < 2 && o_list.length > 0 && e_list.length > 2) {
      return o_list[0];}
      }

Everytime I run it I get this error that says "TypeError: Cannot read property 'length' of undefined at iqTest". 每次运行它时,我都会收到以下错误消息:“ TypeError:无法读取iqTest上未定义的属性'length'”。 Can anyone explain to me how I can fix this error. 谁能向我解释如何解决此错误。 I don't understand why any object would be undefined in my code. 我不明白为什么我的代码中未定义任何对象。 Every var declaration I make is complete. 我所做的每个var声明都是完整的。 I thought undefined only came up if I wrote something like 'var a' without defining it. 我以为只有在我写了类似“ var a”之类的东西的时候才定义未定义。

this one is very easy! 这个很容易!
in these situations you want to trace the value back. 在这些情况下,您想追溯值。

  1. you use .length in iqTest() in the line var len_int = int_list.length; 您可以在var len_int = int_list.length;行的iqTest()中使用.length var len_int = int_list.length;

  2. the last assignment to int_list is at line var int_list = convertToIntegers(numbers); int_list的最后一个赋值是在var int_list = convertToIntegers(numbers);行处var int_list = convertToIntegers(numbers);

  3. glimps at convertToIntegers(numbers) shows no return statement. convertToIntegers(numbers)瞥见没有return语句。

therefore conclusion: 因此得出结论:
convert2Integers() returns nothing into int_list convert2Integers()不向int_list返回任何int_list
then you use int_list.length; 然后您使用int_list.length; of undefined undefined

it seems you wish to return lst from convert2Integers() 似乎您希望从convert2Integers()返回lst

function convertToIntegers(lst) {
  lst = lst.split(' '); // NOTICE CHANGE, + create a different list!
  var len = lst.length;
  var count = 0;
  while (count < len) {
    lst[count] = parseInt(lst[count]);
    count++
  }
  return lst; //THIS LINE was Missing!
}

Tomer W and Patrick Evans are right. Tomer W和Patrick Evans是对的。 change your helper function 改变你的助手功能

function convertToIntegers(lst) {
  lst = lst.split(' ');
  var len = lst.length;
  var count = 0;
  while (count < len) {
    lst[count] = parseInt(lst[count]);
    count++;
  }
  return lst;
}

this way you are storing th result of the split and then returning the array when done. 这样,您将存储拆分结果,然后在完成后返回数组。

that being said this function isn't needed. 就是说不需要此功能。 Check out the built in array function map 查看内置数组功能

To expand on @Patrick Evans comment you need to save the result of the split then use that for your while loop. 要扩展@Patrick Evans注释,您需要保存拆分结果,然后将其用于while循环。 Just as a side note you know the number of iterations of the loop so you can use a for loop. 正如旁注所示,您知道循环的迭代次数,因此可以使用for循环。

function convertToIntegers(lst) {
//lst.split(' ');
var lst_array = lst.split(' ');
var len = lst_array .length;
var count = 0;
while (count < len) {
  lst_array[count] = parseInt(lst_array[count]);
  count++}
  }
//
//for loop code
//
//for(i=0;i<lst_array.length;i++){
//  lst_arry[i] = parseInt(lst_array[i]);
//}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到这个“无法读取未定义的属性”长度”? (JavaScript) - Why am I getting this 'cannot read property 'length' of undefined'? (JavaScript) 谁能告诉我为什么我收到错误'无法读取未定义的属性'值''? P5.JS - Can anyone tell me why i am getting the error 'Cannot read property 'value' of undefined'? P5.JS 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 我不知道为什么我在这里得到“无法读取未定义的属性&#39;长度&#39;的错误”错误 - I can't figure out why I'm getting the “Cannot read property 'length' of undefined” error here 为什么在定义的数组上收到“ ERROR TypeError:无法读取未定义的属性&#39;length&#39;的信息”? - Why am I receiving “ERROR TypeError: Cannot read property 'length' of undefined” on a defined array? 我收到错误Uncaught TypeError:无法读取未定义的属性&#39;drawBox&#39;,有人可以发现问题吗? - I'm getting the error Uncaught TypeError: Cannot read property 'drawBox' of undefined, Can anyone spot the issue? 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 收到此错误:无法读取未定义的属性“长度” - Getting this error: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM