🎮 문제 풀이 및 기억하고 싶은 것들을 임시로 남기는 페이지입니다.
만으로 계산한 나이를 구하는 함수인 getWesternAge 함수를 구현해 봅시다.
이 함수는 birthday 라는 인자를 받습니다.
이 birthday 는 Date 객체 입니다. birthday 라는 인자를 넣었을 때, 현재를 기준으로 만으로 계산한 나이를 리턴 해주세요.
birthday 는 string이 아닌 Date 객체라는 걸 명심하세요 :)
예를 들어, 오늘이 2020년 7월 21일이고, birthday 값이 다음과 같다면:
1990-03-21T00:45:06.562Z
1990-03-21T00:45:06.562Z
리턴 값은 30 이 되어야 합니다.
function getWesternAge(birthday) {
// 1. 현재날짜
const date = new Date();
const getDateYear = date.getFullYear();
const getDateMonth = date.getMonth() +1;
const getDateDay = date.getDate();
// 2. 생일날짜
const myBirthday = new Date(birthday);
const getMyBirdayYear = myBirthday.getFullYear();
const getMyBirthMonth = myBirthday.getMonth() +1;
const getMyBirthDate = myBirthday.getDate();
// 3. 나이계산(현재-생일)
const americanAge = getDateYear - getMyBirdayYear;
// 4. 조건문 발동
if((getMyBirthMonth < getDateMonth) && (getMyBirthDate < getDateDay)) {
return americanAge;
}
else if((getMyBirthMonth >= getDateMonth ) && (getMyBirthDate > getDateDay)) {
return americanAge -1;
}
else {
return americanAge;
}
}