Question: I have javascript array with a teaching hour of a teacher in a day

```
{
"time": [
  { "start":"10:00","end":"11:00" },
  { "start":"12:00","end":"01:00" },
  { "start":"04:00","end":"06:00" }
]
}

```

I want to find out free hour of from above array between **10 AM to 6PM**

Output array will like this :

```
{
"time": [
  { "start":"09:00","end":"10:00" },
  { "start":"11:00","end":"12:30" },
  { "start":"01:00","end":"04:00" }
]
}

```

please help me out to solve this

Answer: 
Chaining like you are currently doing is generally the best solution. It is easy to understand, efficient and (relatively) concise.

A couple of other ideas would be:

* build an array of predicates and then iterate and call them, or
* use exceptions and exception handling

but both of these have performance implications, and they will only give "cleaner" code if you have a vast number of predicates to evaluate.

Sometimes an inelegant solution is more elegant than looking for a clever solution.

---

Consider this: if I use exceptions, I can rewrite the `validate()` method

```
private boolean validate(){
    return checkOne() && checkTow() && checkThree() && checkFour();
}

```

as 

```
private void validate() throws ValidationException {
    checkOne(); checkTow(); checkThree(); checkFour();
}

```

But how much have I actually gained here? It is still two lines of code. And if I were to follow Java's style rules it would be:

```
private void validate() throws ValidationException {
    checkOne(); 
    checkTow(); 
    checkThree(); 
    checkFour();
}

```

which is *more* lines than we started with. And we haven't considered the predicates themselves or the code that handles the validation exception.
Based on my comment: you're probably after exceptions. 

Example (pseudo code):

```
void checkOne() {
  if( check_fails ) {
    throw new CheckOneException();
  }
}

void checkTwo() {
  if( check_fails ) {
    throw new CheckTwoException();
  }
}

void validate() {
  checkOne();
  checkTwo();
}

void SUBMIT_BUTTON_CLICK() {
  try {
    validate();

    //Do Stuff
  } catch( CheckOneException | CheckTwoException ) {
    //handle the exceptions
  }
}

```

Note that you might have to either declare the exceptions to be thrown or make them runtime exceptions. 

Additionally you might want to use multiple catch-blocks if the handling depends on the type of exception. Alternatively you could also throw the same type of exception if that fits your needs.