The ++
operator if used after the variable (Y++
- postfix) returns the value, and then increments it. If used before the value (++Y
- prefix), it increments, and then returns it:
let x = 1, y = 1;
console.log(x++); // returns x then increments
console.log(++y); // increments y and then returns it
However, you don't need to increment the accumulator, return the new value instead:
const reducedValue = array.reduce((acc, curr) =>
acc + (decision(curr) ? 1 : 0)
, 0);
And since you can cast a boolean to a number using the unary +
operator, you can shorten it to:
const reducedValue = array.reduce((acc, curr) =>
acc + decision(curr) // acc + +decision(curr) in typescript
, 0);