Based on the month and date of the birthday, a line of code calculates the JS small function of the constellation

Source: Internet
Author: User

See the other people write a calculation of the constellation JS, a long bit of a look not to the past, I wrote a.
I think this function should be concise enough:)
What if AH switch ah for ah all go away ...
Incoming parameters: month [int] 1~12; day [int] 1~31.

// the constellation is calculated based on the month and date of the birthday. http://blog.csdn.net/cuixiping/

functionGetastro (month,day) {
vars="Capricorn Aquarius Pisces Aries taurus Gemini cancer lion Virgin Libra Scorpio shooter Capricorn";
vararr=[ -, +, +, +, +, A, at, at, at, at, A, A];
returnS.substr (Month*2-( Day<Arr[month-1]?2:0),2);
}

If you search the online "date constellation function", in addition to my or reprint my code, almost is a long code, I am afraid I can not find more concise code than the above.


However, the above 3 lines of code can still be further streamlined, and then to improve:

//  the constellation is calculated based on the month and date of the birthday. http://blog.csdn.net/cuixiping/
function getastro (m,d) {

return "capricorn Aquarius Pisces aries taurus Gemini giant crab lion Virgin libra Scorpio shooter Capricorn". substr (m*2-(d<"102223444433". CharAt ( M-1)--19) *2,2);
}

is not cool, only one line of code, and the maximum simplification.

The above uses a substr function and a charAt function, if you do not consider IE, then. charAt (m-1) can even be reduced to [m-1]

If you do not need to output the constellation name and only need the constellation number index, the preceding string can also be omitted, it looks like this:

//  the constellation is calculated based on the month and date of the birthday. http://blog.csdn.net/cuixiping/
function getastro (m,d) {

return    m (d<"102223444433". CharAt (m-1)--19); //Output 0~12 number, 0 for Capricorn, 1 for water bottle, and so on, ..., 11 is shooter, 12 is Capricorn.
}


Write a test code below to verify:

//  the constellation is calculated based on the month and date of the birthday. http://blog.csdn.net/cuixiping/
function getastro (m,d) {

return "capricorn Aquarius Pisces aries taurus Gemini giant crab lion Virgin libra Scorpio shooter Capricorn". substr (m*2-(d<"102223444433". CharAt ( M-1)--19) *2,2);
}


Write a test function below

function Test (m,d) {
document. Writeln (m+"month"+d+"Day"+getastro (m,d));
}

Test

Test (12,21); //output: December 21 shooter
Test (12,22); //output: December 22 capricorn
Test (+); //output: January 1 capricorn
Test (2,18); //output: February 18 water bottle
Test (2,19); //output: February 19 pisces


With 12 constellation Date control:

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)


According to the month and date of the birthday, a line of code to calculate the constellation JS small function (GO)

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.