JavaScript web page special effects-"fisherman fishing net" program design

When programming, encapsulate the code that may need to be executed repeatedly into a function, and then call it where the function of the code needs to be executed, so that not only the code can be reused, but more importantly, the consistency of the code can be guaranteed. If the function code is modified, all calling locations will be reflected. At the same time, splitting large tasks into multiple functions is also the basic idea of ​​"divide and conquer" and "modular programming", which is conducive to the simplification of complex problems. This section uses a custom function to realize the program design of "fisherman fishing and drying nets".

1 case presentation

If a fisherman starts "fishing for three days and drying nets for two days" starting from January 1 of that year, the program can input a certain day of the year and output whether the fisherman is "fishing" or "drying nets". The input and output effects of the case are shown in Figure 4-10.

 

 Figure 4-10 Case input and output effects

2 case analysis

In the case, starting from January 1 of that year, "fishing for three days and drying nets for two days" started, then fishing was carried out on January 1st, 2nd and 3rd of that year, and netting was carried out on January 4th and 5th of that year. analogy. Firstly, it is calculated that the date entered by the user is the day of the year. Since the cycle of "fishing" and "netting" is 5 days, the calculated number of days is calculated as a remainder of 5. If the remainder is 1, 2, 3, then he is "fishing", otherwise he is "drying the net". According to the analysis, the algorithm design is divided into three steps.

(1) Calculate the input date is the day of the year, if the current year is a leap year and the input month is greater than 2, you need to add one day.

(2) Take the remainder of the calculated number of days to 5.

(3) Judge whether the fisherman is "fishing" or "drying the net" according to the remainder; if the remainder is 1, 2, 3, the fisherman is "fishing", otherwise he is "drying the net".

In order to simplify the code, two functions are customized in the program to realize the functions of judging the leap year and calculating the number of the input date in the current year, and then call it in the program.

3 case realization

  1  <!DOCTYPE html>
  2  <html lang="en">
  3  <head>
  4  </head>
  5  <body>
  6   </script>
  7         function isLeap(year) { // 判断是否闰年
  8             if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
  9                 return true;
 10             }
 11             return false;
 12         }
 13         function getDays(year, month, day) {// 计算计算输入日期是当年的第几天
 14             var arr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];//月份
 15             for (var i = 0; i < month - 1; i++) {// for循环让前面月份天数相加。
 16                 day += arr[i];
 17             }
 18             if (isLeap(year) && month > 2) {// 调用函数
 19                 day++;
 20             }
 21             return day;
 22         }
 23         var year = prompt("请输入年");
 24         var month = prompt("请输入月");
 25         var day = prompt("请输入日");
 26         var n = getDays(year, month, day); // 调用函数
 27         if ((n % 5) < 4 && (n % 5) > 0) // 余数是1或2或3时说明在打渔,否则在晒网
 28             alert(year + "年" + month + "月" + day + "日渔夫在打渔");
 29         else
 30             alert(year + "年" + month + "月" + day + "日渔夫在晒网");
 31     </script>
 32   </body>
 33  </html>

 In the case code, the 7th line of code defines the function isLeap(year), which judges whether the parameter year is a leap year; the 13th line of code defines the function getDays(year, month, day), which calls isLeap(year) to calculate whether the input date is the current year The number of days; the 26th line of code calls the getDays(year, month, day) function to obtain the number of the input date in the current year; the 27th-30th line of code will take the remainder of the obtained number of days to 5, and judge whether the fisherman is in " Fishing" is still "posting the net", and the corresponding result information will be displayed in a pop-up window.


Guess you like

Origin blog.csdn.net/weixin_43396749/article/details/128039796