为什么我的累加器没有初始化?我一直都没有定义。
function addingImages() {
for (var i = 0; i <= ***; i++) {
var image = document.createElement("img");
image.setAttribute("src", "images/1.jpg");
document.querySelector(".gallery").appendChild(image);
}
}
答案 0 :(得分:1)
你需要return the accumulated value from the reducer function,而不是改变它:
var S = "SOSSTSROS";
var radiatedLetters = Array.prototype.reduce.call(S, function(acc, curr) {
if (!curr.match(/[SO]/)) {
return acc + 1;
} else {
return acc;
}
}, 0);
console.log(radiatedLetters);
&#13;