Finding astrological signs based on birthdates using JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in a date object. And based on that object our function should return the astrological sign related to that birthdate.

Example

Following is the code −

 Live Demo

const date = new Date();
// as on 2 April 2021
const findSign = (date) => {
   const days = [21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22];
   const signs = ["Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo",    "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn"];
   let month = date.getMonth();
   let day = date.getDate();
   if(month == 0 && day <= 20){
      month = 11;
   }else if(day < days[month]){
      month--;
   };
   return signs[month];
};
console.log(findSign(date));

Output

Aries

The output may vary based on the time we are running the function because we are using current date.

raja
Published on 17-Apr-2021 13:31:21
Advertisements