JS calculates the constellation's simple realization method according to the birthday month and the date _javascript skill

Source: Internet
Author: User
Tags time and date

The example of this article tells JS based on the birthday month and date to calculate the constellation of the simple implementation method. Share to everyone for your reference, specific as follows:

See someone else write a computational constellation of JS, a little too long to see the past, he wrote a.

I think that this function should be streamlined enough:

What if AH switch ah for ah all fuck off ...

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

Calculates the constellation according to the month and date of the birthday.
function Getastro (month,day) {
  var s= "capricorn Aquarius Pisces aries taurus Gemini giant crab lion Virgin libra scorpio Sagittarius Capricorn";
  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);
}

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

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

Calculates the constellation according to the month and date of the birthday.
function Getastro (m,d) {return
  "Capricorn Aquarius Pisces aries taurus Gemini giant crab lion Virgin libra scorpio Sagittarius Capricorn". substr (m*2-(d< "102223444433". CharAt (M-1) - -19) *2,2);
}

Isn't it cool, there's only one line of code, and the maximum is streamlined.

Using a substr function and a charAt function, if IE is not considered, then. charAt (m-1) can even be reduced to [m-1]

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

Calculates the constellation according to the month and date of the birthday.
function Getastro (m,d) {return
  m (d< "102223444433". CharAt (m-1)- -19);  The number of output 0~12, 0 for Capricorn, 1 for Aquarius, and so on, ... 11 is shooter, and 12 is Capricorn.
}

Write a test code below to verify:

Calculates the constellation according to the month and date of the birthday.
function Getastro (m,d) {return
  "Capricorn Aquarius Pisces aries taurus Gemini giant crab lion Virgin libra scorpio Sagittarius Capricorn". substr (m*2-(d< "102223444433". CharAt (M-1) - -19) *2,2);
}
The following write a test function functions Test
(m,d) {
Document.writeln (m+ "Month" +d+ "Day" +getastro (m,d))
;
Testing Test
(12,21);//output: December 21 Archer
Test (12,22);//output: December 22 capricorn
Test (1,1);   Output: January 1 capricorn
test (2,18);  Output: February 18 aquarius
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)

More readers interested in JavaScript-related content can view the site topics: "JavaScript time and date Operation skills Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"

I hope this article will help you with JavaScript programming.

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.