A toy example of a pattern I am trying to solve elegantly. In the below algorithm, I would like to immediately return 0 from reduce when I find an element with a value of 0 without visiting the remaining elements.
let factors = [2,3,6,0,9,4,4,4];
function product(arr) {
return arr.reduce((acc, elem) => (acc * elem), 1);
}
Is there some way to break out of the reduce iteration?