- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding astrological signs based on birthdates using JavaScript
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 −
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.
Advertisements