0
\$\begingroup\$

I am looking for best practice on how I may write this function more efficiently.

  var d = new Date(),
      time = d.getHours(),
      day  = d.getDay();

  if (day > 0 || day < 7){
    if (time > 18 || time < 8){
      // execute code

    }    
  }                     
\$\endgroup\$
6
  • 2
    \$\begingroup\$ What makes you think your current code is inefficient? \$\endgroup\$ Commented Jan 30, 2012 at 19:20
  • \$\begingroup\$ I thought there may be a way to run everything in one if statement to make it more efficient. I am not really sure just looking to see if I can optimize. \$\endgroup\$
    – jnolte
    Commented Jan 30, 2012 at 19:22
  • \$\begingroup\$ Yes, you can combine the two if statements into one with the logical AND operator &&, but I don't think the efficiency difference will be noticeable, if there is one at all. If you find your current code more readable, there's nothing wrong in keeping it as it is. \$\endgroup\$ Commented Jan 30, 2012 at 19:23
  • 1
    \$\begingroup\$ You are doing something I see a lot of new programmers do, which is worrying about efficiency (to the point of silliness) when there's lots and lots of better stuff a new programmer should be worrying about. Some free advice (that's worth its cost, I'm sure): Don't ask any more questions about efficiency until you know enough to ask questions about efficiency, at which time you will no longer want to ask questions about efficiency. \$\endgroup\$
    – Wayne
    Commented Jan 30, 2012 at 19:28
  • \$\begingroup\$ @lwburk Don't the 2 go hand in hand? I am just trying to learn through example. \$\endgroup\$ Commented Jan 30, 2012 at 22:37

2 Answers 2

1
\$\begingroup\$
if ( (day > 0 || day < 7) && (t > 18 || t < 8))

Is this what you want?

Second condition will only be evaluated only if the first one is true. If the first one is true, then only the second one is evaluated.

\$\endgroup\$
5
  • \$\begingroup\$ yes, this is what I was looking for. Thank you! \$\endgroup\$
    – jnolte
    Commented Jan 30, 2012 at 19:24
  • 2
    \$\begingroup\$ But, (day > 0 || day < 7) makes no sense to me. Why would you want to do that? \$\endgroup\$ Commented Jan 30, 2012 at 19:25
  • \$\begingroup\$ I do not want my code to run on Sat / Sun. Is this wrong? \$\endgroup\$
    – jnolte
    Commented Jan 30, 2012 at 19:27
  • \$\begingroup\$ @jnolte, Saturday is day 6, since days are zero-based. \$\endgroup\$ Commented Jan 30, 2012 at 19:29
  • \$\begingroup\$ @jnolte Have a look at this. sprunge.us/NGKi I guess, this is what you want. \$\endgroup\$ Commented Jan 30, 2012 at 19:35
0
\$\begingroup\$

Change the first conditional to

if(day>0)

The Date.getDay() function always returns an integer between 0 - 6 so it will never be greater than or equal to seven.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.