-1

Instead of writing like "28 or 29 days", how can we add also the leap-year check to switch, to get 29 days for the year 2000?

var month = "Feb";
var year = 2000;
switch(month) {
    case "Apr": case "Jun": case "Sep": case "Nov":
    console.log("30 days."); break;
case "Feb":
    console.log("28 or 29 days."); break;
default: console.log("31 days.");
}
  • Does this answer your question? [Calculate last day of month in JavaScript](https://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript) – Ivar Jan 10 '21 at 00:20

2 Answers2

1

Hope this helps you. Code should be self explanatory. Please let me know.

function isLeapYear(year) {
  return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

function getMonthLength(month, year) {
  switch(month) {
    case "Apr": case "Jun": case "Sep": case "Nov":
      return 30;
    case "Feb":
      return isLeapYear(year) ? 29 : 28;
    default:
      return 31;
  }
}

console.log(getMonthLength('Feb', 2000));
console.log(getMonthLength('Jan', 2001));
console.log(getMonthLength('Feb', 2001));
console.log(getMonthLength('Sep', 2001));
Ernesto Stifano
  • 3,027
  • 1
  • 10
  • 20
  • Dear Ernesto, your answer works seamlessly, thanks. Later I post the question, I find a solution by myself, but yours is better. – Ahmet Arduç Jan 10 '21 at 01:35
  • @AhmetArduç I'm glad to hear that. Please remember to mark this question as answered if you think we're done here. – Ernesto Stifano Jan 10 '21 at 12:44
-1

Knowing that 2020 was a leap year write a function like this

 var findLeap = ()=> {
    var dateYear = new Date().getFullYear();
    var leap = dateYear - 2020
 
    if (leap % 4 == 0){
      console.log('Its another Leap year')
  }else{
      console.log('Its NOT a Leap year')
        }
}
Ogoh.cyril
  • 462
  • 7
  • 14