I'm currently working on finding an elegant solution to a toy example pattern. The goal is to stop the reduce algorithm immediately and return 0 when an element with a value of 0 is encountered, without processing the rest of the elements.
let factors = [2,3,6,0,9,4,4,4];
function product(arr) {
return arr.reduce((acc, elem) => (acc * elem), 1);
}
Is there a way to exit the reduce iteration early?