Last active
September 27, 2020 07:47
-
-
Save riljian/1447c82b82a1cf310b772196fe3dbdab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const zodiac = [ | |
{ name: '牡羊', startMonth: 3, startDate: 21, endMonth: 4, endDate: 19, }, | |
{ name: '金牛', startMonth: 4, startDate: 20, endMonth: 5, endDate: 20, }, | |
{ name: '雙子', startMonth: 5, startDate: 21, endMonth: 6, endDate: 20, }, | |
{ name: '巨蟹', startMonth: 6, startDate: 21, endMonth: 7, endDate: 22, }, | |
{ name: '獅子', startMonth: 7, startDate: 23, endMonth: 8, endDate: 22, }, | |
{ name: '處女', startMonth: 8, startDate: 23, endMonth: 9, endDate: 22, }, | |
{ name: '天秤', startMonth: 9, startDate: 23, endMonth: 10, endDate: 22, }, | |
{ name: '天蠍', startMonth: 10, startDate: 23, endMonth: 11, endDate: 22, }, | |
{ name: '射手', startMonth: 11, startDate: 23, endMonth: 12, endDate: 21, }, | |
{ name: '魔羯', startMonth: 12, startDate: 22, endMonth: 1, endDate: 19, }, | |
{ name: '水瓶', startMonth: 1, startDate: 20, endMonth: 2, endDate: 18, }, | |
{ name: '雙魚', startMonth: 2, startDate: 19, endMonth: 3, endDate: 20, }, | |
] | |
// 目的:輸入生日並找出對應星座,印出該星座的名稱 | |
// 方法:日期轉變為一串數字再找出對應的星座範圍 | |
// eg. birthday = 6/24. 6/24 >>> 624 | |
// 巨蟹為6/21-7/22 >>> 621 - 722 | |
// birthday = 624, 介於621與722之間 | |
// console.log (巨蟹) | |
// 指派生日,並拆成年/月/日 | |
const birthday = '1994/12/21' | |
const birthdayArr = birthday.split('/') | |
// 將 birthdayArr 的資料轉成數字,並使用calcDateValue函式加總 (函式在底端) | |
const month = Number(birthdayArr[1]) | |
const date = Number(birthdayArr[2]) | |
const birthdayValue = calcDateValue (month, date) | |
let found = false | |
for (let i = 0; i < zodiac.length; i++) { | |
const startValue = calcDateValue(zodiac[i].startMonth, zodiac[i].startDate) | |
const endValue = calcDateValue(zodiac[i].endMonth, zodiac[i].endDate) | |
if (birthdayValue >= startValue && birthdayValue <= endValue) { | |
console.log (zodiac[i].name) | |
found = true | |
} | |
} | |
if (!found) { | |
console.log('摩羯') | |
} | |
// 將生日轉為數字,例如11/22 >>> 1122 | |
function calcDateValue (m, d) { | |
return m *100 +d | |
} | |
// Question | |
// 對於摩羯座這個特例是否有其他條件式的寫法,在迴圈內找出來 | |
// 有試過先跑迴圈,不相符的再用else印出摩羯。但會變成每個不符合的星座都重複印出摩羯 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment