[JS Practice] Christmas day

Title :

Christmas day

Description:

Sometimes it’s useful to know on which day of week the holly holiday will occur.
But just don’t limit ourselves to this year and write a function which consumes a Christmas date and outputs a name of weekday.

Example

findOutChristmasWeekday('2013 12 25') // returns 'Wednesday'

Only valid Christmas dates will be passed to the function.

Date parameter could be a string or a Date object. If it’s a string here are possible date parameter formats:

'2013 12 25'
'12 25 2013'
'25 December 2013'

Note: calendar used in the kata is Gregorian.

My solution :

function findOutChristmasWeekday(date) {
  var d = new Date(date);
  var week;
  
  switch(d.getDay()) {
    case 0 :
      week = "Sunday";break;
    case 1 :
      week = "Monday";break;
    case 2 :
      week = "Tuesday";break;
    case 3 :
      week = "Wednesday";break;
    case 4 :
      week = "Thursday";break;
    case 5 :
      week = "Friday";break;
    case 6 :
      week = "Saturday";break;
  }
  return week;
}

Best solution :

function findOutChristmasWeekday(date) {
  var day = new Date(date).getDay();
  return ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day]
}

Ref. from codewars

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s