# Constellation judgment blog

### Preface

My love for the front end can be extended to my love for life. This is my sixth week in the anti war class. Starting to learn the scripting language JavaScript makes my front-end development more likely. I think programming learning should not be limited to the classroom, and the best way to learn is to be able to do some simple function realization with my own ideas. So this week I tried to compile a simple program with my knowledge of JavaScript to judge the constellation corresponding to the input date.

### Preparation

Before we start to write code, we need to make a simple analysis of our own needs. The first thing we have to understand is the distribution of constellation.

Constellation name Aries Taurus Gemini Cancer Leo Virgo
Corresponding date 3.21 - 4.19 4.20 - 5.20 5.21 - 6.21 6.22 - 7.22 7.23 - 8.22 8.23 - 9.22
Constellation name Libra Scorpio Sagittarius Capricornus Aquarius Pisces
Corresponding date 9.23 - 10.23 10.24 - 11.22 11.23 - 12.21 12.22 - 1.19 1.20 - 2.18 2.19 - 3.20

### Compilation idea

Train of thought 1: take constellation as the object to find its corresponding range
As can be seen from the above table, the date of each constellation basically starts around the 20th of the current month and ends around the 20th of the next month.
For example, to judge Aquarius from January 20 to February 18, we need to judge whether the month "is January, whether the date is greater than 20 and less than 31" and "whether it is February, whether the date is less than 18" meet one of the conditions, and output "constellation is Aquarius". The code is as follows:

```var getMonth = parseInt(prompt("Please enter the month")), getDay = parseInt(prompt("Please enter the date"));
if ((getMonth == 1 && getDay >= 20 && getDay <= 31)||(getMonth == 2 && getDay <= 18)) {
}
```

It can be seen from the above codes that if you write judgment conditions directly for each constellation, you need to consider not only the month, but also the date, and because the days of each month are different, you need to judge the rationality of the date, which is too redundant.

Idea 2: take month as the object to judge the constellation corresponding to the corresponding date
For example, to judge the constellation of February, the code is as follows:

```var getMonth = parseInt(prompt("Please enter the month")), getDay = parseInt(prompt("Please enter the date"));
switch (getMonth){
case 2:
if(getDay >= 1 && getDay <= 18 ){
}else if(getDay >= 18 && getDay <= 29 ){
}break;
}
```

It is much easier to judge whether the input date is within the maximum number of days of the current month, and then judge the date of the current month to determine the constellation.

### Code simplification

Although we get the desired results from the above code, there are many repetitions in the code used to judge the date range and pop-up prompt. In order to enhance the readability of the code and save computer resources, we can simplify it appropriately.
We might as well declare a container to store the variables used to control the pop-up results. When the conditions are met in the loop, we can modify the variables, which can be written as

```var getMonth = parseInt(prompt("Please enter the month")), getDay = parseInt(prompt("Please enter the date"));
var res = null; //Declare a separate container for constellation output.
switch (getMonth){
case 2:
if(getDay >= 1 && getDay <= 18 ){
res = 'Aquarius';
}else if(getDay >= 18 && getDay <= 29 ){
res = 'Pisces';
}break;
}
```

After further observation, the number of days in each month ranges from 1 to 31, and some months only have 30 or 29 days, so it's better to wrap an if judgment statement outside the switch cycle to screen out the number of days that do not meet the requirements, and then define the judgment formula according to the maximum number of days in several special months, so the following 12-month code can be written.

```var getMonth = parseInt(prompt("Please enter the month")), getDay = parseInt(prompt("Please enter the date"));
var res = null; //Declare a separate container for constellation output.
var lastDay = (getDay <= 30),FebDay = (getDay <= 29); //Used to limit the maximum days of the month
if(getDay >= 1 || getDay <= 31){
switch (getMonth){
case 1://1Moon constellation
if(getDay <= 19){res = 'Capricornus';}else if(getDay >= 20){res = 'Aquarius';}break;
case 2: //February constellation
if(getDay <= 18){res = 'Aquarius';}else if(getDay >= 18 && FebDay){res = 'Pisces';}break;
case 3://3Moon constellation
if(getDay <= 20){res = 'Pisces';}else if(getDay >= 21){res = 'Aries';}break;
case 4: //April constellation
if(getDay <= 19){res = 'Aries';}else if(getDay >= 20 && lastDay){res = 'Taurus';}break;
case 5://5Moon constellation
if(getDay <= 20){res = 'Taurus';}else if(getDay >= 21){res = 'Gemini';}break;
case 6://6Moon constellation
if(getDay <= 21){res = 'Gemini';}else if(getDay >= 22 && lastDay){res = 'Cancer';}break;
case 7://7Moon constellation
if(getDay <= 22){res = 'Cancer';}else if(getDay >= 23){res = 'Leo';}break;
case 8://8Moon constellation
if(getDay <= 22){res = 'Leo';}else if(getDay >= 23){res = 'Virgo';}break;
case 9://9Moon constellation
if(getDay <= 22){res = 'Virgo';}else if(getDay >= 23 && lastDay){res = 'Libra';}break;
case 10://10Moon constellation
if(getDay <= 23){res = 'Libra';}else if(getDay >= 24){res = 'Scorpio';}break;
case 11://11Moon constellation
if(getDay <= 22){res = 'Scorpio';}else if(getDay >= 23 && lastDay){res = 'Sagittarius';}break;
case 12://12Moon constellation
if(getDay <= 21){res = 'Sagittarius';}else if(getDay >= 22){res = 'Capricornus';}break;
}
}
```

Finally, there is also a need to pay attention to the problem of floating-point numbers. Theoretically, the date and month must be integers. How can visitors prompt for input errors after entering decimal numbers? In fact, we only need to add a simple judgment, that is, rounding the input number, and then judge whether the rounding result is equal to that before rounding.

```var getMonth = prompt("Please enter the month"), getDay = prompt("Please enter the date");
if(getMonth == parseInt(getMonth) && getDay == parseInt(getDay)){
getMonth = parseInt(getMonth), getDay = parseInt(getDay);
zodiac();
}

function zodiac(){
var res = null; //Declare a separate container for constellation output.
var lastDay = (getDay <= 30),FebDay = (getDay <= 29); //Used to limit the maximum days of the month
if(getDay >= 1 || getDay <= 31){
switch (getMonth){
case 1://1Moon constellation
if(getDay <= 19){res = 'Capricornus';}else if(getDay >= 20){res = 'Aquarius';}break;
case 2: //February constellation
if(getDay <= 18){res = 'Aquarius';}else if(getDay >= 18 && FebDay){res = 'Pisces';}break;
case 3://3Moon constellation
if(getDay <= 20){res = 'Pisces';}else if(getDay >= 21){res = 'Aries';}break;
case 4: //April constellation
if(getDay <= 19){res = 'Aries';}else if(getDay >= 20 && lastDay){res = 'Taurus';}break;
case 5://5Moon constellation
if(getDay <= 20){res = 'Taurus';}else if(getDay >= 21){res = 'Gemini';}break;
case 6://6Moon constellation
if(getDay <= 21){res = 'Gemini';}else if(getDay >= 22 && lastDay){res = 'Cancer';}break;
case 7://7Moon constellation
if(getDay <= 22){res = 'Cancer';}else if(getDay >= 23){res = 'Leo';}break;
case 8://8Moon constellation
if(getDay <= 22){res = 'Leo';}else if(getDay >= 23){res = 'Virgo';}break;
case 9://9Moon constellation
if(getDay <= 22){res = 'Virgo';}else if(getDay >= 23 && lastDay){res = 'Libra';}break;
case 10://10Moon constellation
if(getDay <= 23){res = 'Libra';}else if(getDay >= 24){res = 'Scorpio';}break;
case 11://11Moon constellation
if(getDay <= 22){res = 'Scorpio';}else if(getDay >= 23 && lastDay){res = 'Sagittarius';}break;
case 12://12Moon constellation
if(getDay <= 21){res = 'Sagittarius';}else if(getDay >= 22){res = 'Capricornus';}break;
}  