我正在尝试实现函数式编程,我开始使用forEach并将reduce减少到给定数组中的零(在雄辩的JavaScript第5章中出现问题)。但我不明白为什么我的代码在某些情况下有效但在其他情况下无效。
下面的代码不起作用,而忽略else语句似乎是个问题。但是为什么在这里需要else语句而不是在帖子结尾发布的某些案例中呢?
function forEach(arr, action) {
for(var i = 0; i < arr.length; i++) {
action(arr[i]);
}
}
function reduce(cb, start, array) {
forEach(array, function(el) { start = cb(start, el); });
return start;
}
function countZeroes(array) {
function counter(total, element) {
if(element === 0) return total + 1; //excluding else statement seems to be the issue but why?
}
return reduce(counter, 0, array);
}
console.log(countZeroes([0,1,0,2,0,3, 0,4,2,0])); //returns NaN
如果我使用else语句重写函数,它可以工作:
function forEach(arr, action) {
for (var i = 0; i < arr.length; i++) {
action(arr[i]);
}
}
function reduce(cb, start, array) {
forEach(array, function(el) {
start = cb(start, el);
});
return start;
}
function countZeroes(arr) {
function counter(total, element) {
if (element === 0) {
return total + 1;
} else {
return total;
}
}
return reduce(counter, 0, arr);
}
console.log(countZeroes([0, 1, 2, 0]));
但是,代码在下面的情况下不需要else语句,为什么?
function countTarget(arr, target) {
var counter = 0;
for(var i = 0; i < arr.length; i++) {
if(arr[i] === target) counter++; //no else statement required here
}
return counter;
}
console.log(countTarget([0,1, 0, 0, 2, 3, 0, 3, 0, 0, 0], 0));
function forEach(arr, action) {
for(var i = 0; i < arr.length; i++) {
action(arr[i]);
}
}
function test(a) {
return a === 0;
}
function countTarget(arr, target) {
var counter = 0;
forEach(arr, function(el) {
if (test(el)) counter++;
});
return counter;
}
console.log(countTarget([1, 2, 3, 0, 3, 0], 0));
我会很感激。
答案 0 :(得分:0)
function counter(total, element) {
if(element === 0) return total + 1; //excluding else statement seems to be the issue but why?
}
在此函数中,当total + 1
为0时,您返回一些值(element
)。因此对于其他每种情况,此函数都返回undefined,因为您没有为{{1}指定任何值} 案件。
else
+ undefined
= number
你可以像这样检查NaN
的返回值
counter
forEach(array, function(el) {
console.log(start);
start = cb(start, el);
});
记录的值
console.log(countZeroes([0,1,0,2,0,3, 0,4,2,0]));
0
1
undefined
等等
NaN
记录的值
console.log(countZeroes([0,1,0,2,0,3, 0,4,2,0]));
0
1
1
2
2
等等
定义下面的函数时
3
javascript将其实现为
function f_name() {
// calculations
return some_value;
}
function f_name() {
// calculations
return some_value;
return undefined; // implied by javascript
}
在执行流程中遇到function
并且其余代码无法访问时,可以有多个return
语句和函数返回。
所以,在你的情况下
return
当function counter(total, element) {
if(element === 0) return total + 1; //excluding else statement seems to be the issue but why?
return undefined; // implied
}
值为element
时,函数返回0
,其余代码(total + 1
)变得无法访问,但return undefined;
不是{{} 1}},该函数执行element
。
希望这能澄清你的怀疑。
答案 1 :(得分:0)
从counter
函数返回的内容是传递给counter
的下一个调用。因此,您需要在计数器函数上返回某些。如果条件不满意,则返回未受影响的总数:
function counter(total, element) {
return (element === 0) ? total + 1 : total;
}
说明:您的counter
函数根据某些逻辑递增“total”值并返回递增的值。然后,返回的值再次传递给counter
函数。您的计数器函数具有不返回值的代码路径(当if语句未通过时)。在这种情况下,您没有返回任何内容......这与返回undefined
类似。如果你要孤立它,它看起来会像这样:
// Let's say your current "total" count is 15
var total = counter(15, 2);
console.log(total); //-> undefined
从2 !== 0
开始 - 条件没有通过,函数的结果是undefined
。现在,下次调用计数器时,它将如下所示:
var total = counter(undefined, 0);
从0 === 0
开始,条件将通过,并且会尝试增加总数... undefined
:
return undefined + 1; //-> NaN
希望有所帮助。