-1

I am new to javascript. I want to write a program which tells the number of days in a month, considering leap year.

// 18. Write a program which tells the number of days in a month, now consider leap year.

let month = parseInt(prompt('Enter Month', 'Eg. 1'))
let year = parseInt(prompt('Enter year', '2023'))

function calculateDays(month, year) {
    if (isNaN(month) || isNaN(year)) {
        alert('Please Enter in Digit Only')
    }
    else if (month <= 0 || month > 12) {
        alert(`Please Enter Month Between 1 to 12`)
    }
    else if ((month === 1) || (month === 01) || (month === 3) || (month === 03) || (month === 5) || (month === 05) || (month === 7) || (month === 07) || (month === 8) || (month === 08) || (month === 10) || (month === 12)) {
        alert(`Number of days in ${month} are 31`)
    }
    else if ((month === 2 || month === 02) && (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0)) {
        alert(`Number of days in ${month} are 29`)
    }
    else if (month === 2 || month === 02) {
        alert(`Number of days in ${month} are 28`)
    }
    else {
        alert(`Number of days in ${month} are 30`)
    }
}

I don't know what mistake i am making. Kindly help me to solve this problem.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • You should explain what is the expected and current behaviour and how they are different – Konrad Jan 12 '23 at 17:36
  • 4
    `1` and `01` are the same number, you don't need to compare with both of them. – Barmar Jan 12 '23 at 17:37
  • 1
    Remove anything with leading zeros. Your code is invalid in strict mode. I strongly encourage you to move to [JavaScript modules](//developer.mozilla.org/en/docs/Web/JavaScript/Guide/Modules) or at least enable [strict mode](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Strict_mode). Use linters like [JSHint](//jshint.com) to find problems with your code immediately. Use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`), read any errors. See [What is a debugger and how can it help me diagnose problems?](/q/25385173/4642212). – Sebastian Simon Jan 12 '23 at 17:40

1 Answers1

2

You created the function calculateDays, but you didn't call it. Here's the fixed code:

// 18. Write a program which tells the number of days in a month, now consider leap year.

let month = parseInt(prompt('Enter Month', 'Eg. 1'))
let year = parseInt(prompt('Enter year', '2023'))

calculateDays(month, year) // <---- HERE IS THE FUNCTION CALLED

function calculateDays(month, year) {
    if (isNaN(month) || isNaN(year)) {
        alert('Please Enter in Digit Only')
    }
    else if (month <= 0 || month > 12) {
        alert(`Please Enter Month Between 1 to 12`)
    }
    else if ((month === 1) || (month === 01) || (month === 3) || (month === 03) || (month === 5) || (month === 05) || (month === 7) || (month === 07) || (month === 8) || (month === 08) || (month === 10) || (month === 12)) {
        alert(`Number of days in ${month} are 31`)
    }
    else if ((month === 2 || month === 02) && (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0)) {
        alert(`Number of days in ${month} are 29`)
    }
    else if (month === 2 || month === 02) {
        alert(`Number of days in ${month} are 28`)
    }
    else {
        alert(`Number of days in ${month} are 30`)
    }
}
FireFragment
  • 596
  • 1
  • 4
  • 15