Skip to content
Advertisement

Guard clause on lists using only functional programming

The problem I am facing is that, given a list and a guard condition, I must verify if every element in the list passes the guard condition.

If even one of the elements fails the guard check, then the function should return false. If all of them pass the guard check, then the function should return true. The restriction on this problem is that I can only use a single return statement.

My code:

JavaScript

Advertisement

Answer

You should use all:

JavaScript

Or in a more functional way:

JavaScript

For example for range 0 to 9 (range(10)):

JavaScript
Advertisement