user1115666
user1115666

Reputation:

Calling a function on Christmas day

I'm having a little bit of trouble trying to make it 'snow' only on Christmas day. I figured out how to grab the day but I'm not sure how make it call SnowStorm() correctly.

var christmas = {
  month: 11,
  date: 25
}

function isItChristmas() {
  var now = new Date();
  var isChristmas = (now.getMonth() == christmas.month && now.getDate() == christmas.date);

if (isChristmas)
    return ;
  else
    return ;
}

var snowStorm = null;

function SnowStorm() {
    (snowstorm code)

}

snowStorm = new SnowStorm();

Upvotes: 4

Views: 1941

Answers (3)

Sherzod
Sherzod

Reputation: 5131

Here's the version if you are planning to make a function (not a class):

var christmas = {
  month: 12,
  date: 25
}

function isItChristmas() {
  var now = new Date();
  return (now.getMonth() == christmas.month && now.getDate() == christmas.date);
}

if (isItChristmas()){
    SnowStorm();
  }else{
    //not a christmas
}
    
function SnowStorm() {
    (snowstorm code)
}

Here's the one if you are planning to make a class:

var christmas = {
  month: 12,
  date: 25
}

function isItChristmas() {
  var now = new Date();
  return (now.getMonth() == christmas.month && now.getDate() == christmas.date);
}

var storm = new SnowStorm();

if (isItChristmas()){
    storm.Snow();
  }else{
    //not a christmas
}
    
function SnowStorm() {
    this.Snow = function(){
        (snowstorm code)
    }
}

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039238

You need to return a boolean value from the isItChristmas function:

function isItChristmas() {
   var now = new Date();
   return (now.getMonth() + 1 == christmas.month && 
           now.getDate() == christmas.date);
}

and then call it:

function SnowStorm() {
    if (isItChristmas()) {
        // your snowstorm code here
        // that will execute only on Christmas
    }
}

By the way you will notice that the .getMonth() method returns the month from 0 to 11, so you need to add 1 as shown in my example, otherwise your code will run on 25th of November.

Upvotes: 0

LoneWolf
LoneWolf

Reputation: 505

First, you are not returning anything on the isItChristmas function. Second, in the SnowStorm function just add

if (isItChristmas) {
    (your code here)
}

Upvotes: 0

Related Questions