

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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.
- Related Questions & Answers
- Finding quarter based on month index in JavaScript
- Finding number plate based on registration number in JavaScript
- Finding life path number based on a date of birth in JavaScript
- Finding the height based on width and screen size ratio (width:height) in JavaScript
- Finding the 1-based index of a character in alphabets using JavaScript
- Encrypting a string based on an algorithm using JavaScript
- Inverting signs in array - JavaScript
- Inverting signs of integers in an array using JavaScript
- JavaScript Separate objects based on properties
- Finding pandigital numbers using JavaScript
- Calculating value of a sequence based on some input using JavaScript
- Reorder array based on condition in JavaScript?
- Change string based on a condition - JavaScript
- Splitting strings based on multiple separators - JavaScript
- Sorting Array based on another array JavaScript
Advertisements