2

I have such a probelm. It consists of two parts

  1. Write a JS code that determines whether a given year is a leap year.

  2. Write a JS code for a program that determines the number of days in the given month of a given year by utilizing the solution to the previous problem as a predefined process/function call.

Thank you in advance.

  • 2
    https://stackoverflow.com/questions/1184334/get-number-days-in-a-specified-month-using-javascript – Dov Rine Oct 10 '21 at 06:26
  • Does this answer your question? [javascript to find leap year](https://stackoverflow.com/questions/8175521/javascript-to-find-leap-year) – Arvind Oct 10 '21 at 06:57

1 Answers1

-1

This should work:

function isLeapYear(year){
  return(year%4==0);
}
console.log(isLeapYear(2024));
console.log(isLeapYear(2023));
function daysInMonth(month,year){
  if (month!=2){
    a=[1,3,5,7,8,10,12]
    for (x in a){
      if (month==a[x]){
        return(31)
      }
    }
    return(30);
  }else{
    if (isLeapYear(year)){
      return(29);
    }else{
      return(28)
    }
  }
}
console.log(daysInMonth(2,2024))
console.log(daysInMonth(2,2023))
console.log(daysInMonth(1,2023))
console.log(daysInMonth(4,2023))

[edit] Sorry about that. Anyway, the first function returns true if the year is a leap year. The second function returns the amount of days in the specified month of the year. That should be better.

Agent Biscutt
  • 712
  • 4
  • 17
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Oct 10 '21 at 06:35
  • Year that are divideable by 400 are not leap years, except those years also divideable by 2000. Also both July and August have 31 days. – connexo Oct 10 '21 at 06:37