HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Functions between certain times of the day

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thebetweentimesfunctionscertainday

Problem

I'm working on something for my school's laptops that will disable videos/games between 8:00 AM and 3:15 PM. Due to the giant time gap, I will not be waiting to see if the code functions properly or not. What I want to know is if my code will function properly, as I'm not good enough at JavaScript to know myself.

function timer() {
    var date = new Date();
    var hours = date.getHours();
    var minutes = date.getMinutes();

    if (minutes  8 & hours Can you do us all a favor and not eat up all the bandwidth with your video watching?" + "" + "Thank you!" + "" + "");
        $("embed").hide();
        $("embed").parent().html("This is no time for childish games." + "" + "Shouldn't you be studying or doing something related to school?" + "" + "");
    } else if(hours > 15 & hours < 8) {
        $("video").show();
        $("embed").show();
    }
}

Solution

I will comment on this part...

if (hours > 8 & hours Can you do us all a favor and not eat up all the bandwidth with your video watching?" + "" + "Thank you!" + "" + "");
    $("embed").hide();
    $("embed").parent().html("This is no time for childish games." + "" + "Shouldn't you be studying or doing something related to school?" + "" + "");
} else if(hours > 15 & hours < 8) {
    $("video").show();
    $("embed").show();
}


As Hosch said, you should be using the conditional operator &&.

Also, the else if clause wouldn't work because (hours > 15 && hours 15 AND `hours 15 || hours 8 && hours ) and "lesser than" (=) and "lesser or equal" (<=).

In order to cover the whole range you specified, you should write something like:

if ((hours >= 8 && hours < 15) || (hours == 15 && minutes <= 15)) { ... }

Code Snippets

if (hours > 8 & hours < 15) {
    $("video").hide();
    $("video").parent().html("<b>Can you do us all a favor and not eat up all the bandwidth with your video watching?</b>" + "<br/>" + "<b>Thank you!</b>" + "<br/>" + "<br/>");
    $("embed").hide();
    $("embed").parent().html("<b>This is no time for childish games.</b>" + "<br/>" + "<b>Shouldn't you be studying or doing something related to school?</b>" + "<br/>" + "<br/>");
} else if(hours > 15 & hours < 8) {
    $("video").show();
    $("embed").show();
}
else { ... }
if ((hours >= 8 && hours < 15) || (hours == 15 && minutes <= 15)) { ... }

Context

StackExchange Code Review Q#86767, answer score: 2

Revisions (0)

No revisions yet.