Javascript OR operator not working in if statement

后端 未结 6 1431
礼貌的吻别
礼貌的吻别 2020-12-11 23:38

I\'m trying to get this Javascript to do something if the day of the week matches any of the days listed in my statement, as well as restricting it to between 17:00 and 19:0

6条回答
  •  一生所求
    2020-12-12 00:25

    In this case, you better use a range check, because you need only two comparisons against of three or more - and it is better maintanable, just to change a value, if necessary.

    if (dayOfWeek >= 4 && dayOfWeek <= 6 && hour >= 17 && hour < 19) {
    

    The right OR conditions needs parenthesis, because of the precedence of && over ||

    if ((dayOfWeek == 4 || dayOfWeek == 5 || dayOfWeek == 6) && hour >= 17 && hour < 19 ) {
    

提交回复
热议问题