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

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -