Is this way of reducing an array (counting occurrences) not recommended? I mean modifying the accumulator is maybe bad?
function countOccurrencesReduceOwn(array, searchElement) //seems to work but is maybe not recommended ?
{
const count = array.reduce((accumulator, currentValue) => {
if(currentValue === searchElement)
{
accumulator++; //do not do this?
}
return accumulator;
}, 0);
return count;
}
Another similar is the following code which does not work. It's meant for getting the largest value of the array.
function getMaxReduceOwn(array) //does not work
{
if(array.length<=0)
{
return undefined;
}
const largest = array.reduce((largest, currentValue) => {
if(currentValue > largest)
{
largest =currentValue; //do not do this?
}
return largest;
}, array[0]);
}