Skip to content
Advertisement

Get Possible Order Date Based On Multiple Conditions

I can’t wrap my head around this particular problem.

There is a bakery, and an office that accepts orders.

Bakery is open from Monday to Sunday, it’s only closed during public holidays.

Orders can be placed from Monday to Friday unless it’s a public holiday i.e. the office is open during working days.

We also need to distinguish if the user managed to order goods before or after a particular time, let’s say 12:00.

A few examples:

  1. It’s 22.7.2022 (Friday) at 10:00 – orders can be placed for the following days Saturday(23.7), Sunday(24.7), Monday(25.7), etc.
  2. It’s 22.7.2022 (Friday) at 13:00 – orders can be placed for the following days Tuesday(26.7), Wednesday(27.7), Thursday(28.7), etc.
  3. It’s 20.7.2022 (Wednesday) at 10:00 – orders can be placed for the following days Thursday(21.7), Friday(22.7), Saturday(23.7), etc.
  4. It’s 20.7.2022 (Wednesday) at 13:00 – orders can be placed for the following days Friday(22.7), Saturday(23.7), Sunday(24.7), etc.
  5. It’s 23.7.2022 (Saturday) and 24.7.2022 (Sunday) anytime – orders can be placed for the following days Tuesday(26.7), Wednesday(27.7), Thursday(28.7), etc.
  6. It’s 26.7.2022 (Monday) at 10:00 and 27.7(Tuesday) and 28.7(Wednesday) are public holidays – orders can be placed for the following days Thursday(28.7), Friday(29.7), Saturday(30.7), etc.
  7. It’s 26.7.2022 (Monday) at 13:00 and 27.7(Tuesday) and 28.7(Wednesday) are public holidays – orders can be placed for the following days Friday(29.7), Saturday(30.7), Sunday(31.7), etc.

This is what I’ve got so far, but the code isn’t working as expected.

JavaScript

Advertisement

Answer

enter image description here

I’d like to share my own solution, which I ended up with.

Maybe, it’ll help someone in the future.

What helped me a ton was drawing all the possible combinations in graphs, then everything became much clearer and easier.

I think most of the code is self-explanatory, I tried to be as verbose with the function names and variables as possible.

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