function getZodiacNumber(birthMonth, birthDay) {
const zodiacSigns = [
{ name: '魚座', number: 12, start: [2, 19], end: [3, 20] },
{ name: '牡羊座', number: 1, start: [3, 21], end: [4, 19] },
{ name: '牡牛座', number: 2, start: [4, 20], end: [5, 20] },
{ name: '双子座', number: 3, start: [5, 21], end: [6, 21] },
{ name: '蟹座', number: 4, start: [6, 22], end: [7, 22] },
{ name: '獅子座', number: 5, start: [7, 23], end: [8, 22] },
{ name: '乙女座', number: 6, start: [8, 23], end: [9, 22] },
{ name: '天秤座', number: 7, start: [9, 23], end: [10, 23] },
{ name: '蠍座', number: 8, start: [10, 24], end: [11, 22] },
{ name: '射手座', number: 9, start: [11, 23], end: [12, 21] },
{ name: '山羊座', number: 10, start: [12, 22], end: [1, 19] },
{ name: '水瓶座', number: 11, start: [1, 20], end: [2, 18] }
];
for (const zodiac of zodiacSigns) {
const [startMonth, startDay] = zodiac.start;
const [endMonth, endDay] = zodiac.end;
if (
(birthMonth === startMonth && birthDay >= startDay) ||
(birthMonth === endMonth && birthDay <= endDay) ||
(birthMonth > startMonth && birthMonth < endMonth)
) {
return zodiac.number;
}
}
return null; // 該当しない場合
}
// 例: 誕生日が4月15日なら牡羊座(1)
const birthMonth = 4;
const birthDay = 15;
const zodiacNumber = getZodiacNumber(birthMonth, birthDay);
console.log(`星座番号: ${zodiacNumber}`);
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
Comments
星座番号(1~12)のみが取得できればいいのであれば、こんな感じでいいと思います。
無効な日付を渡した場合には
null
を返したいならLet's comment your feelings that are more than good