now.getDay() ranges

I have an assignment for my beginners to programming class and I have to write a program which will determine if I can sleep in or not. I have to “get up” during all weekdays except on three holidays(New Years, July 4th, and Christmas. I am having issues with using the now.getDay() for a range of 1-5. I tried setting it to not being day 0 or 6 but couldn’t get that to work correctly either. I appreciate you all being patient with me as it is probably an overly simple reason for not working as desired. Below is my code.

Here is my code for when I tried to use if equals Saturday or Sunday to Sleep in. also ignore that the message in the if statements looks off. I’ve been flipping them back and forth as I’ve been testing.

Can you do us all a favour and post actual code, rather than images? That way, it will help members recreate your code without needing to manually transcribe it it (hint: hey won’t). If you paste your code in, select it, then click the </> icon on the edit window, it will all get correctly formatted.

Here’s that if statement that you said to ignore.

if (dayOfWeek == 0 || 6) {

That condition will check two different conditions, and if either of those conditions are true it will do what’s inside of the curly braces.

The first condition that it checks is dayOfWeek == 0 which will be true if it’s Sunday.
The second condition is 6, which as it’s not zero will always be true.

A more correct formulation of that if condition is to compare dayOfWeek with 6 as well.

if (dayOfWeek == 0 || dayOfWeek == 6) {

An even better solution is to use triple equals === because if dayOfWeek is undefined, JavaScript considers that undefined == 0 is true. Using triple equals, undefined === 0 is false, which is closer to what we expect.

if (dayOfWeek === 0 || dayOfWeek === 6) {

It’s considered best-practice in JavaScript to always use triple equals for comparisons.

Even better is to assign names to the days of the week, so that the condition becomes much easier to understand.

const SUNDAY = 0;
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
if (dayOfWeek === SUNDAY || dayOfWeek === SATURDAY) {

You can even make it easier to understand, by using a well named function, such as isWeekend()

function isWeekend(dayOfWeek) {
    return dayOfWeek === SUNDAY || dayOfWeek === SATURDAY;
if (isWeekend(dayOfWeek)) {

I will do that in the future. My first time posting I couldn’t figure that out, so thank you for explaining how to paste the code directly. I will do that in the future.

1 Like

What I need to accomplish is for it to say “Sleep in.” If it is a weekend or one of those holidays. If it is a weekday and not one of those holidays I need it to say “Get up!” When I make the changes you suggested it is still giving me a sleep in even though it is Monday(1). Any other suggestions?

<html lang="en-us">
<meta charset="utf-8">
<title>Can I sleep In?</title>

<script type="text/javascript">
/* Input: Only receive input of date from computer
 * Processing: Check date of computer to determine if I can sleep in.
 * Output: "Get up!" If during weekday, if not then "Sleep in."
function sleepIn() {
    // Get the input from computer calendar/clock.
	var now = new Date();
    var month = now.getMonth();
    var dayOfMonth = now.getDate();
    var dayOfWeek = now.getDay();
	var newYearsDay = now.getMonth(1) && now.getDate(1);
	var independenceDay = now.getMonth(7) && now.getDate(4);
	var Christmas = now.getMonth(12) && now.getDate(25);
	var holiday

    // Choose a message.
    if (dayOfWeek === 0 || dayOfWeek === 6) {
		message = "Sleep in." ;
		else if(holiday === (newYearsDay || independenceDay) || Christmas){
		message = "Sleep in." ;
		message = "Get up!"

    // Display the message to the user.
    // document.getElementById('outputDiv').innerHTML = message;
	var div = document.getElementById ('output');
	div.innerHTML = message;

<h1> Should I sleep in?</h1>                    

<button type="button" onclick="sleepIn()">???</button>
<div id="output"></div>              

You could check if it is a weekend or a public holiday to show the sleep in message, otherwise show the get up one.

For example:

if (isWeekend(dayOfWeek) || isHoliday(dayOfWeek)) {
  // sleep in
} else {
  // wake up

thank you.

I’ll work on this with your suggestions.

The variable “message” wasn’t declared, but any way, there are two identical “Sleep in.”s

I think if you changed the second to “Holiday!”, you would see that message.

Why do I think that? What arguments do getMonth and getDate take?

In testing I’ve now narrowed it down to the else if() statement. When I adjust it to only one of the holidays it functions as desired. Tell me where I can better this section of my code please.

else if(holiday === (newYearsDay || independenceDay) || Christmas){
		message = "Sleep in." ;

One of the faults of that code is that (newYearsDay || independenceDay) will be interpreted just as being true, so holiday === (true) will never be true because holiday currently is undefined.

One of the ways of doing things is to use a separate function for each thing,

For example:

VAR DECEMBER = 11; // zero based, with January being 0
function isChristmas(date) {
    return date.getMonth() === DECEMBER && date.getDate() === 25;

You can then use that function to check if it is Christmas.

if (isChristmas(now)) {
    // do Christmas stuff

Normally I don’t like using comments in code because they are a symptom of other issues, that can be better fixed by improving the code.

If I were to do this without comments, it would be to create an array of months, and then compare the month with their index position.

const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

function compareDay(date, day) {
    return date.getDate() === day;
function compareMonth(date, month) {
    return date.getMonth() === months.indexOf(month);
function compareDayAndMonth(date, day, month) {
    return compareDay(date, day) && compareMonth(date, month);

function isChristmas(date) {
    return compareDayAndMonth(date, 25, "December");

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.