0

I am setting up some JavaScript to compare the current time to a venue's opening and closing hours (potentially different each day, so different var for each day).

Here's what I have so far:

// Compare current time to today's hours
if (day === 1 && time > monOpen && time < monClose) {
    venueIsOpen();
} else if (day === 2 && time > tuesOpen && time < tuesClose) {
    venueIsOpen();
} else if (day === 3 && time > wedOpen && time < wedClose) {
    venueIsOpen();
} else if (day === 4 && time > thursOpen && time < thursClose) {
    venueIsOpen();
} else if (day === 5 && time > friOpen && time < friClose) {
    venueIsOpen();
} else if (day === 6 && time > satOpen && time < satClose) {
    venueIsOpen();
} else if (day === 0 && time > sunOpen && time < sunClose) {
    venueIsOpen();
} else {
    venueIsClosed();
}

Obviously very straightforward - is there any way to optimise this?

j_d
  • 2,818
  • 9
  • 50
  • 91
  • 2
    Why do you have so many variables? Shouldn't you put the open and close times in an array of size 7? An "optimisation" would be quite straightforward then. – Bergi Nov 20 '15 at 14:59
  • 3
    http://codereview.stackexchange.com/ – j08691 Nov 20 '15 at 14:59
  • Review/refactoring requests are offtopic here. Go to [CodeReview](http://codereview.stackexchange.com/). – hindmost Nov 20 '15 at 15:10

2 Answers2

2

First of all, we can combine a load of those if statements:

// Compare current time to today's hours
if (day === 1 && time > monOpen && time < monClose ||
    day === 2 && time > tuesOpen && time < tuesClose ||
    day === 3 && time > wedOpen && time < wedClose ||
    day === 4 && time > thursOpen && time < thursClose ||
    day === 5 && time > friOpen && time < friClose ||
    day === 6 && time > satOpen && time < satClose ||
    day === 0 && time > sunOpen && time < sunClose) {
        venueIsOpen();
} else {
    venueIsClosed();
}

That's still quite ugly, right? But there's some logic in there... We could use arrays for the open / close times:

var open = [sunOpen, monOpen, tuesOpen, wedOpen, thursOpen, friOpen, satOpen],
    close = [sunClose, monClose, tuesClose, wedClose, thursClose, friClose, satClose];

if(time > open[day] && time < close[day])
    venueIsOpen();
else
    venueIsClosed();

Or, even shorter using a ternary condition:

(time > open[day] && time < close[day] ? venueIsOpen : venueIsClosed)();
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

Here's another way that could shorten your code:

// These are the 7 days of the week
var openings = [
{opening: 1,close: 2},
{opening: 2,close: 4},
{opening: 5,close: 7},
{opening: 1,close: 5},
{opening: 1,close: 3},
{opening: 2,close: 9},
{opening: 1,close: 2}
];

// loop through all of them
for(var i=0, l=openings.length; i<l; i++) {
    // if the current time is bigger than the current day opening time and smaller than the current closing time call venueIsOpen(), otherwise call venueIsClosed()
    (openings[day].opening < time && openings[day].close > time) ? venueIsOpen() : venueIsClosed();   
}
Jonas Grumann
  • 10,438
  • 2
  • 22
  • 40
  • `var openings = { open: [], close: [] }` would be shorter, but this is also a good option. – Cerbrus Nov 20 '15 at 15:08
  • True, I just think the other way around makes it more readable but since the question is about optimization I guess you're right. – Jonas Grumann Nov 20 '15 at 15:10