forked from tecg-dcc/js-td-basics-4-fonctions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyLib.js
More file actions
41 lines (38 loc) · 987 Bytes
/
myLib.js
File metadata and controls
41 lines (38 loc) · 987 Bytes
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
function isBissextile(year) {
if (year % 4 === 0 && year % 100 !== 0) {
return true;
}
if (year % 400 === 0) {
return true;
}
return false;
}
function getMaxDayPerMonth(month, year) {
if (isNaN(month) || month < 1 || month > 12 || isNaN(year) || year < 1) {
console.error(`Month must be [1-12]`);
console.error(`Year must be >0`);
return -1;
}
let maxDays = 31;
switch (month) {
case 4:
case 6:
case 9:
case 11:
maxDays = 30;
break;
case 2:
if (isBissextile(year) === true) {
maxDays = 29;
} else {
maxDays = 28;
}
}
return maxDays;
}
function isValid(day, month, year) {
if (isNaN(day) || isNaN(month) || isNaN(year) || day <= 0 || month <= 0 || year <= 0 || month > 12) {
return false;
}
return day > 0 && day <= getMaxDayPerMonth(month, year);
}