Higher Order Functions

forEach Does Not Break

forEach Does Not Break

A return inside a forEach callback exits only that callback run. It does not return from the outer function, and it does not make forEach stop visiting later items.

That mistake can hide in search-like code because the predicate looks reasonable at a glance. If you still use forEach, the outer function needs its own variable to hold the first match, and later callback runs must not overwrite it.

Your task: Fix firstLongLabel so it returns the first label longer than eight characters. Return null when no label qualifies.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

forEach Does Not Break

A return inside a forEach callback exits only that callback run. It does not return from the outer function, and it does not make forEach stop visiting later items.

That mistake can hide in search-like code because the predicate looks reasonable at a glance. If you still use forEach, the outer function needs its own variable to hold the first match, and later callback runs must not overwrite it.

Your task: Fix firstLongLabel so it returns the first label longer than eight characters. Return null when no label qualifies.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Console

Run code to see output.