Skip to content
Advertisement

Seeking logic to scan array for next active time slot

I’m trying to define the logic that would return a value of the next live show time per day for a media station.

The array

JavaScript

Process

JavaScript

All works fine to that point.

Now I want to show a notice when there is no live host on air so I’ve used current(), next() and end() to cycle the $start array. It works to a point but fails because the array count() is not consistent per day.

JavaScript

The output notice when retrieved is:

From midnight

The next live show is at 6:00AM

Between first show and next

The next live show is at 1:00PM

After the 1600 (4PM) show end time, the process fails and the output is without the 5PM value for the next in array and just…

The next live show is


How do I correctly scan the array and compare to current time to check if a show is on, and if not, show the time of the next show in line?

Advertisement

Answer

I think it’s quite cumbersome to use current/next in your case. You can simplify your code greatly by keeping track on whether any show is on air $isOnAir = true/false (in below code) and if not, simply reiterating the shows array with a different condition.

Example:

JavaScript

https://3v4l.org/ZKcjo

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement