1

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]);
  }
Suvi
  • 11
  • 2
  • There's nothing strictly forbidden about modifying the value stored in a function parameter. Of course, depending on whether it was a value type or an object type, the usual rules apply. `++` obviously applies to a value type, so the argument supplied by the caller won't be affected. I'm wondering if your question is similar to [Is it a bad practice to modify function arguments in JavaScript](https://stackoverflow.com/questions/41260682/is-it-a-bad-practice-to-modify-function-arguments-in-javascript) – Wyck Jul 22 '22 at 13:24
  • 1. To "count" matches in an array: `arr.filter(x => x === 'searchItem')?.length ?? 0` 2. To find the largest value: `arr.sort((a, b) => b - a)?.[0] ?? 'not found'` (where `arr` is an array of numbers, and it may get reordered/sorted). One need not use `.reduce()` for both these scenarios (though, these may be accomplished using it, too). – jsN00b Jul 22 '22 at 13:25
  • Oh I noticed that the latter one was missing ``return largest``! Silly me! That is why it was not working ! – Suvi Jul 22 '22 at 13:27

1 Answers1

1

Yes, this is unusual. Apart from "modifying parameters", reduce comes from functional programming where mutable variable are despised. You should rather simply return a new value:

const count = array.reduce((accumulator, currentValue) => {
  if (currentValue === searchElement) return accumulator + 1;
  else return accumulator; 
}, 0);
const largest = array.reduce((largest, currentValue) => { 
  if (currentValue > largest) return currentValue;
  else return largest;
});

If you were to reassign the accumulator variable, there's no reason to use the reduce method at all, you could simply write a normal for loop to the same effect, which would be shorter and more idiomatic than the unusal reduce:

let count = 0;
for (const currentValue of array) {
  if(currentValue === searchElement) {
    count++;
  }
}
return count;
if (array.length <= 0) {
  return undefined;
}
let largest = array[0];
for (const currentValue of array.slice(1)) {
  if (currentValue > largest) {  
    largest = currentValue;
  }
}
return largest;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375