Reputation: 191
Why doesn't my accumulator get initialized? I keep getting undefined.
var S = "SOSSTSROS";
var radiatedLetters = Array.prototype.reduce.call(S,function(acc,curr){
if(!curr.match(/[SO]/)){
acc++;
}
},0);
console.log(radiatedLetters);
Upvotes: 0
Views: 142
Reputation: 42460
You need to return the accumulated value from the reducer function, not mutate it:
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);
Upvotes: 1