It should be necessary to have interaction, and Js is chosen to implement it, which can be regarded as a first test of pair programming. I wrote the display part in html, and the button clicked trigger event function is check();
The code copy is as follows:
function onCheck(){
var Year = document.getElementById(year).value; //Get the "year" of the text box var theYear =Year * 1; //Convert to number type//alert(theYear); //Get monthly value
var month = document.getElementById(month);
var index1=month.selectedIndex; var theMonth = month.options[index1].value; //Get the month value
var day = document.getElementById(day);
var index2=day.selectedIndex;
var theDay = day.options[index2].value;
// Input value judgment part
...
//Calling core functions
days(theYear, theMonth, theDay);
}
The core function days are as follows:
The code copy is as follows:
function days(year,month,day) {
var days = 0; //Indicate what day the change date is the same as that of the year
//Accumulated number of months
for(var i = 1; i < month; i++ ){
switch(i){
//The situation of the big month is 31 more
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:{
days += 31;
break;
}
//Xiaoyue's situation adds 30
case 4:
case 6:
case 9:
case 11:{
days += 30;
break;
}
//The situation in February will be added according to the year type
case 2:{
if(isLeapYear(year)){
days += 29; //Add 29 in leap year
}
else {
days += 28;
}
break;
}
}
}
day = day * 1;
days += day; //The sum of the number of days plus the number of days
var date0 = new Date(year,0,1); //The first day of that year was the week
// alert(date0.getDay());
var date1 = new Date(year,month-1,day); //Format the date value, 0-11 represents January-December;
// alert((days + date0.getDay()+6)/7);
var nthOfWeek = Math.floor((days + date0.getDay()+6)/7); //Summ down
// alert(nthOfWeek);
var toDay = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
//day.getDay(); Return a certain Sunday in a week according to Date 0 of which is 0 of the week
alert("This date is the "+days+"day/n"+" of the year is the "+nthOfWeek+" weekly"+toDay[date1.getDay()]);
}
During the debugging process, many unexpected errors were encountered, such as calculation errors caused by type mismatch, such as rounding of numbers;
With the assistance of his teammates, he is responsible for reviewing and assisting in catching bugs, and I am responsible for implementing and coding;
In the last part, in the test of input values, we assisted each other well, analyzed different input situations, covered various possible accidents, and quickly completed the improvement of the functions;
The following is the code to determine whether the input value is allowed:
The code copy is as follows:
if (isNaN(theYear)|| theYear < 0) {
alert("Input is incorrect, please re-enter");
return ;
}
if((theMonth == 2 && theDay > 29 && isLeapYear(theYear))||(theMonth == 2 && theDay > 28 && !isLeapYear(theYear))) {
alert("Input is incorrect, please re-enter");
return ;
}
if((theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11) && theDay == 31 ) {
alert("Input is incorrect, please re-enter");
return ;
}