Checking Every Runner With some() and every()

Your port can now turn the export’s rows into runner records. Before publishing, the timing desk asks three questions about the field: did anyone drop out, which finishers are missing a chip read, and which runner has the bib number a marshal has called in? Python answers each in one line with any(), all(), and next(). JavaScript has all three, but as array methods.

Put the Test in the Call

JavaScript has no any() or all() built-ins. Arrays have .some() and .every() methods instead. Each takes a test function, as .filter() does, and checks whether its result is truthy. Here are the desk’s first two questions side by side:

Question Python JavaScript
Anyone DNF? any(r["finish"] == "DNF" for r in runners) runners.some((r) => r.finish === "DNF")
Reads all there? all(r["checkpoints"]) r.checkpoints.every((t) => t !== "")

Python hands a built-in a stream of values to check for truthiness. JavaScript calls your test on each item, so .every() needs one even where all() takes the list as it is. With these string reads, t !== "" is that test. Both methods stop once the result is determined, as any() and all() do.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.