Skip to content

Instantly share code, notes, and snippets.

@jdberrocal1
Last active September 30, 2021 00:48
Show Gist options
  • Save jdberrocal1/d17e0a5a601274f4f795cb0050695894 to your computer and use it in GitHub Desktop.
Save jdberrocal1/d17e0a5a601274f4f795cb0050695894 to your computer and use it in GitHub Desktop.
Get Horoscope Sign By DOB (Javascript)
const SIGN_NAMES = [
{
sign: 'Aquarius',
start: {
day: 20,
month: 1,
},
end: {
day: 18,
month: 2,
}
},
{
sign: 'Pisces',
start: {
day: 19,
month: 2,
},
end: {
day: 20,
month: 3,
}
},
{
sign: 'Aries',
start: {
day: 21,
month: 3,
},
end: {
day: 19,
month: 4,
}
},
{
sign: 'Taurus',
start: {
day: 20,
month: 4,
},
end: {
day: 20,
month: 5,
}
},
{
sign: 'Gemini',
start: {
day: 21,
month: 5,
},
end: {
day: 20,
month: 6,
}
},
{
sign: 'Cancer',
start: {
day: 21,
month: 6,
},
end: {
day: 22,
month: 7,
}
},
{
sign: 'Leo',
start: {
day: 23,
month: 7,
},
end: {
day: 22,
month: 8,
}
},
{
sign: 'Virgo',
start: {
day: 23,
month: 8,
},
end: {
day: 22,
month: 9,
}
},
{
sign: 'Libra',
start: {
day: 23,
month: 9,
},
end: {
day: 22,
month: 10,
}
},
{
sign: 'Scorpio',
start: {
day: 23,
month: 10,
},
end: {
day: 21,
month: 11,
}
},
{
sign: 'Sagittarius',
start: {
day: 22,
month: 11,
},
end: {
day: 21,
month: 12,
}
},
{
sign: 'Capricorn',
start: {
day: 22,
month: 12,
},
end: {
day: 19,
month: 1,
}
},
];
function horoscopeSignName(dob) {
if (dob && dob.month && dob.day) {
const startLimitSign = getLimitSignByMonth(dob.month, true);
const endLimitSign = getLimitSignByMonth(dob.month, false);
if ((dob.month === startLimitSign.start.month && dob.day < startLimitSign.start.day) ||
(dob.month === endLimitSign.start.month && dob.day < endLimitSign.start.day)) {
return startLimitSign;
}
return endLimitSign;
}
}
function getLimitSignByMonth(month, isStart) {
return SIGN_NAMES.find(sign => {
return isStart ? sign.end.month === month : sign.start.month === month;
});
}
// examples
console.log(horoscopeSignName({month:2, day:18})) //Aquarius
console.log(horoscopeSignName({month:3, day:21})) //Aries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment