admin管理员组文章数量:1022670
I'm doing this challenge on codewars, it passed all the test but I got an error:
TypeError: Cannot read property 'length' of null
so I can't past the challenge. Can anyone tell me what's wrong with this code ?
function averages(numbers) {
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
I'm doing this challenge on codewars, it passed all the test but I got an error:
TypeError: Cannot read property 'length' of null
so I can't past the challenge. Can anyone tell me what's wrong with this code ?
function averages(numbers) {
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
Share
Improve this question
edited Feb 21, 2017 at 21:04
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Feb 10, 2017 at 12:54
MikeMike
1033 gold badges4 silver badges11 bronze badges
2
-
well, what do you expect
length
to be ifnull
is passed as parameter? – UnholySheep Commented Feb 10, 2017 at 12:55 -
you're passing
numbers
to your function, wich should be an array, if not, it will show that error. So be carefull about always passing an array, even if it's only 1 number. – LordNeo Commented Feb 10, 2017 at 12:58
4 Answers
Reset to default 3There is an important part of the instructions that you aren't handling:
If the array has 0 or 1 values or is null or None, your method should return an empty array.
function averages(numbers) {
if (!numbers) return []; // return empty array
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
You just need to test if the parameter is null
//test if numbers is null
if(numbers == null){
return [];
}
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
function averages(numbers) {
if(numbers)
{
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
else
{
return [];
}
}
Just add a null check to ur numbers variable. Then you are good to go.
To correct your code:
function averages(numbers) {
if (!Array.isArray(numbers)) {
return [];
}
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
I use Array.isArray
to check that it is really an array. It is an inbuilt function, so you should use it.
You can check this and the length in one step, saving more steps (look the example below).
To provide a cleaner solution:
function averages(numbers) {
if (!Array.isArray(numbers) || numbers.length < 2) {
return [];
}
for(var i = 0; i < numbers.length - 1; i++){
numbers[i] = (numbers[i] + numbers[i + 1]) / 2;
}
numbers.pop();
return numbers
}
It works fine, I also added a "-1" to the loop, as the last number will bee "NaN" (Not a Number), because there is no element after it and (10 + undefined) / 2 => NaN / 2 => NaN
. This also adds an unnecessary step to your algorithm.
To make it plete
Another solution might be Array:map:
function averages(numbers) {
if (!Array.isArray(numbers) || numbers.length < 2) {
return [];
}
numbers.map(function(val, idx, arr) {
arr[idx] = (val + arr[idx+1]) / 2;
});
numbers.pop();
return numbers;
}
I'm doing this challenge on codewars, it passed all the test but I got an error:
TypeError: Cannot read property 'length' of null
so I can't past the challenge. Can anyone tell me what's wrong with this code ?
function averages(numbers) {
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
I'm doing this challenge on codewars, it passed all the test but I got an error:
TypeError: Cannot read property 'length' of null
so I can't past the challenge. Can anyone tell me what's wrong with this code ?
function averages(numbers) {
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
Share
Improve this question
edited Feb 21, 2017 at 21:04
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Feb 10, 2017 at 12:54
MikeMike
1033 gold badges4 silver badges11 bronze badges
2
-
well, what do you expect
length
to be ifnull
is passed as parameter? – UnholySheep Commented Feb 10, 2017 at 12:55 -
you're passing
numbers
to your function, wich should be an array, if not, it will show that error. So be carefull about always passing an array, even if it's only 1 number. – LordNeo Commented Feb 10, 2017 at 12:58
4 Answers
Reset to default 3There is an important part of the instructions that you aren't handling:
If the array has 0 or 1 values or is null or None, your method should return an empty array.
function averages(numbers) {
if (!numbers) return []; // return empty array
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
You just need to test if the parameter is null
//test if numbers is null
if(numbers == null){
return [];
}
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
function averages(numbers) {
if(numbers)
{
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2 || numbers[i] === ""){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
else
{
return [];
}
}
Just add a null check to ur numbers variable. Then you are good to go.
To correct your code:
function averages(numbers) {
if (!Array.isArray(numbers)) {
return [];
}
for(var i=0; i<numbers.length; i++){
if(numbers.length < 2){
return [];
} else {
numbers[i] = (numbers[i] + numbers[i+1]) / 2;
}
}
numbers.pop();
return numbers
}
I use Array.isArray
to check that it is really an array. It is an inbuilt function, so you should use it.
You can check this and the length in one step, saving more steps (look the example below).
To provide a cleaner solution:
function averages(numbers) {
if (!Array.isArray(numbers) || numbers.length < 2) {
return [];
}
for(var i = 0; i < numbers.length - 1; i++){
numbers[i] = (numbers[i] + numbers[i + 1]) / 2;
}
numbers.pop();
return numbers
}
It works fine, I also added a "-1" to the loop, as the last number will bee "NaN" (Not a Number), because there is no element after it and (10 + undefined) / 2 => NaN / 2 => NaN
. This also adds an unnecessary step to your algorithm.
To make it plete
Another solution might be Array:map:
function averages(numbers) {
if (!Array.isArray(numbers) || numbers.length < 2) {
return [];
}
numbers.map(function(val, idx, arr) {
arr[idx] = (val + arr[idx+1]) / 2;
});
numbers.pop();
return numbers;
}
本文标签: Cannot read property 39length39 of null in JavaScriptStack Overflow
版权声明:本文标题:Cannot read property 'length' of null in JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745497777a2153246.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论