-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path18. Q18.js
More file actions
47 lines (41 loc) · 1.1 KB
/
18. Q18.js
File metadata and controls
47 lines (41 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 18. Write a program which tells the number of days in a month, now consider leap year.
const prompt = require("prompt-sync")({ sigint: true });
// First Approach
// let getDaysInMonth = () => {
// const month = prompt("Enter the month in numbers: ");
// const year = prompt("Enter the year: ");
// return new Date(year, month, 0).getDate();
// };
// console.log(getDaysInMonth());
// Another Approach
let getDaysInMonth2 = () => {
const monthNum = prompt("Enter the month in numbers: ");
if (monthNum > 12 || monthNum < 1) {
console.log("Enter number between 1 to 12");
getDaysInMonth();
}
if (monthNum <= 7) {
if (monthNum == 2) {
const year = prompt("Enter the year: ");
if (
(year % 100 == 0 && year % 400 == 0) ||
(year % 100 != 0 && year % 4 == 0)
) {
console.log(29);
} else {
console.log(28);
}
} else if (monthNum % 2 != 0) {
console.log(31);
} else {
console.log(30);
}
} else {
if (monthNum % 2 == 0) {
console.log(31);
} else {
console.log(30);
}
}
};
getDaysInMonth2();