I'm currently working on a new feature that will allow users to set multiple wake alarms for specific days and times. For example, they may want alarms to go off on Mondays, Wednesdays, and Fridays at 7:00 AM and 8:00 PM, and on Tuesdays and Thursdays at 6:00 AM and 9:00 PM.
One of the challenges I'm facing is determining the most efficient way to store this data in order to:
- Display all the set days/times including options for adding or removing alarms.
- Create a function that can determine the next scheduled time based on the current time. For instance, if it's Monday at 8:05 PM, the function should return Tuesday at 6:00 AM.
Initially, I tried storing the days and times in an array like this:
{
"0":["07:00","20:00"],
"1":["06:00","21:00"],
"2":["07:00","20:00"],
...
}
In this setup, "0" represents the value returned by getDay()
for Monday. While this approach made it easy to access times for a specific day, it proved challenging to calculate the next scheduled time efficiently. My initial solution involved using a forEach() loop to iterate through the array and compare the current day/time until finding the next scheduled alarm, but it felt cumbersome and inefficient.
Do you have any suggestions for a more streamlined solution, perhaps utilizing Angular or a specific library?