Home > Web Front-end > JS Tutorial > JS simple implementation method to calculate zodiac sign based on birthday month and date

JS simple implementation method to calculate zodiac sign based on birthday month and date

高洛峰
Release: 2016-12-05 13:31:02
Original
2466 people have browsed it

The example in this article describes the simple implementation method of calculating the zodiac sign based on the birthday month and date using JS. I would like to share it with you for your reference. The details are as follows:

I saw a js for calculating zodiac signs written by someone else. It was too long to read, so I wrote it myself.

I think this function should be concise enough :)

Pass in parameters: month [int] 1~12; day [int] 1~31.

// 根据生日的月份和日期,计算星座。
function getAstro(month,day){
  var s="魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
  var arr=[20,19,21,21,21,22,23,23,23,23,22,22];
  return s.substr(month*2-(day<arr[month-1]?2:0),2);
}
Copy after login

If you search for "date constellation function" on the Internet, except for mine or reprinted my code, almost all of them are long codes. I am afraid that you cannot find any more concise code than the above.

However, the above 3 lines of code can still be further streamlined. Let’s improve it again:

// 根据生日的月份和日期,计算星座。
function getAstro(m,d){
  return "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(m*2-(d<"102223444433".charAt(m-1)- -19)*2,2);
}
Copy after login

Isn’t it cool? There is only one line of code, and it is streamlined to the maximum extent.

A substr function and a charAt function are used above. If IE is not considered, then .charAt(m-1) can even be simplified to [m-1]

If you do not need to output the constellation name but only need the constellation numerical index, The previous string can also be omitted, it looks like this:

// 根据生日的月份和日期,计算星座。
function getAstro(m,d){
  return m-(d<"102223444433".charAt(m-1)- -19);  //输出0~12的数字,0表示摩羯,1表示水瓶,依此类推,...,11是射手,12是摩羯。
}
Copy after login

Write a test code below to verify:

// 根据生日的月份和日期,计算星座。
function getAstro(m,d){
  return "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(m*2-(d<"102223444433".charAt(m-1)- -19)*2,2);
}
//下面写一个测试函数
function test(m,d){
document.writeln(m+"月"+d+"日 "+getAstro(m,d));
}
//测试
test(12,21); //输出: 12月21日 射手
test(12,22); //输出: 12月22日 魔羯
test(1,1);   //输出: 1月1日 魔羯
test(2,18);  //输出: 2月18日 水瓶
test(2,19);  //输出: 2月19日 双鱼
Copy after login

Attached is the date comparison of the twelve zodiac signs:

Capricorn ( 12/22 - 1/19), Aquarius (1/20 - 2/18), Pisces (2/19 - 3/20), Aries (3/21 - 4/20),
Taurus (4/ 21 - 5/20), Gemini (5/21 - 6/21), Cancer (6/22 - 7/22), Leo (7/23 - 8/22),
Virgo (8/23 - 9/ 22), Libra (9/23 - 10/22), Scorpio (10/23 - 11/21), Sagittarius (11/22 - 12/21)


Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template