Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[js]誕生日から日本で一般的な星座を割り出す

Posted at
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}`);
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
shiba_mitue

@shiba_mitue(mitue shiba)

息子が8歳で母プログラマ。フロントエンジニア。vue/nuxt,firebase,gcpがメイン。

Comments

oswe99489
@oswe99489
(Edited)

星座番号(1~12)のみが取得できればいいのであれば、こんな感じでいいと思います。

const getZodiacNumber = (month, day) => ([
  '0321', '0420', '0521', '0622', '0723', '0823',
  '0923', '1024', '1123', '1222', '0120', '0219',
].filter(v => v <= `0${month}`.slice(-2) + `0${day}`.slice(-2)).length + 9) % 12 + 1;

console.log(`星座番号: ${getZodiacNumber(4, 15)}`); // 星座番号: 1

無効な日付を渡した場合にはnullを返したいなら

const getZodiacNumber = (month, day) => (
  month = `${month}`.padStart(2, '0'),
  day = `${day}`.padStart(2, '0'),
  new Date(`2000-${month}-${day}`).getDate() === +day && ([
    '0321', '0420', '0521', '0622', '0723', '0823',
    '0923', '1024', '1123', '1222', '0120', '0219',
  ].filter(v => v <= `${month}${day}`).length + 9) % 12 + 1) || null;

console.log(`星座番号: ${getZodiacNumber( 0,  0)}`); // 星座番号: null
console.log(`星座番号: ${getZodiacNumber( 4, 15)}`); // 星座番号: 1
console.log(`星座番号: ${getZodiacNumber( 5, 31)}`); // 星座番号: 3
console.log(`星座番号: ${getZodiacNumber( 6, 31)}`); // 星座番号: null
console.log(`星座番号: ${getZodiacNumber(13,  1)}`); // 星座番号: null
console.log(`星座番号: ${getZodiacNumber(12, 31)}`); // 星座番号: 10
console.log(`星座番号: ${getZodiacNumber(12, 32)}`); // 星座番号: null
console.log(`星座番号: ${getZodiacNumber(13,  1)}`); // 星座番号: null
0

Let's comment your feelings that are more than good

Qiita Conference 2024 Autumn will be held!: 11/14(Thu) - 11/15(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

Takahiro Anno, Masaki Fujimoto, Yukihiro Matsumoto(Matz), Shusaku Uesugi / Nicolas Ishihara(Vercel Inc.)

View event details

Being held Article posting campaign

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address