Which fix removes the bug?
const found = [1, 2, 3].forEach((n) => {
if (n === 2) return n;
});
- Use
findso the match is returned - Use
filterso the match is returned - Add
returnbefore theforEachcall - Change the arrow to a normal function
forEach always returns undefined and a return inside it only exits that callback; find returns the match.
Play Problem Solving ▸