[英]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. 在这些情况下,您想追溯值。
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;
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);
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.