javascript - Accumulator not initialized when using reduce.call -
why doesn't accumulator initialized? 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);
you need return accumulated value 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);
Comments
Post a Comment