Simple implementation of JS constellation calculation based on birthday month and date

Source: Internet
Author: User
JS simple implementation of Constellation calculation based on the birthday month and date this article describes the simple implementation of JS constellation calculation based on the birthday month and date. We will share this with you for your reference. The details are as follows:

I saw a js of a computing constellation written by someone else. If it was too long, I wrote it myself.

I think this function should be simplified enough :)

Input parameter: month [int] 1 ~ 12; day [int] 1 ~ 31.

// Calculate the Constellation Based on the month and date of the birthday. Function getAstro (month, day) {var s = ""; var arr = [20, 19, 21, 23, 23,23, 22,22]; return s. substr (month * 2-(day

If you search for the "date constellation function" on the Internet, except for my code or the code I reproduced, it is almost a long code, and I am afraid I cannot find code that is more concise than above.

However, the above three lines of code can be further streamlined and further improved:

// Calculate the Constellation Based on the month and date of the birthday. Function getAstro (m, d) {return "Libra Scorpio shooter ". substr (m * 2-(d <"102223444433 ". charAt (m-1)--19) * 2, 2 );}

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 taken into account, the. charAt (S-1) function can be reduced to M-1].

If you do not need to output a constellation name but only need a digital index of the constellation, you can save the preceding string, which looks like this:

// Calculate the Constellation Based on the month and date of the birthday. Function getAstro (m, d) {return m-(d <"102223444433". charAt (S-1)--19); // output 0 ~ The number of 12, 0 indicates Capricorn, 1 indicates water bottle, and so on,..., 11 indicates shooter, and 12 indicates Capricorn .}

Write a test code to verify the Code:

// Calculate the Constellation Based on the month and date of the birthday. Function getAstro (m, d) {return "Libra Scorpio shooter ". substr (m * 2-(d <"102223444433 ". charAt (m-1)--19) * 2, 2);} // write a test function test (m, d) {document. writeln (m + "month" + d + "day" + getAstro (m, d);} // test (); // output: shooter test (); // output: Magic test (); // output: water bottle test ); // output: Pisces, September February 19

Appendix 12 constellation date comparison:

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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.