프로그래머스/Lv.1
프로그래머스 JS [개인정보 수집 유효기간]
sakuraop
2023. 6. 24. 20:47
문제 요약
오늘의 날짜가 2020.02.02 이고,
약관 A는 5달 동안 유효할 때,
["2019.01.01 D", "2019.11.15 Z", "2019.08.02 D", "2019.07.01 D", "2018.12.28 Z"]
약관이 만료된 요소는 몇 번째 인지 모두 반환하시오.
function solution(today, terms, privacies) {
const result = [];
const termsObject = {};
terms.forEach((term) => (termsObject[term.split(" ")[0]] = term.split(" ")[1]));
privacies.forEach((privacy, index) => {
const [date, term] = privacy.split(" ");
const expireDate = new Date(date);
expireDate.setMonth(expireDate.getMonth() + parseInt(termsObject[term]));
if (new Date(today) >= expireDate) {
result.push(index + 1);
}
});
return result;
}
solution(
"2020.01.01",
["Z 3", "D 5"],
["2019.01.01 D", "2019.11.15 Z", "2019.08.02 D", "2019.07.01 D", "2018.12.28 Z"]
);
1. 약관을 오브젝트로 만듭니다.
const termsObject = {};
terms.forEach((term) => (termsObject[term.split(" ")[0]] = term.split(" ")[1]));
{ Z: '3', D: '5' } termsObject
2. Date 객체를 이용하여 계약일자+약관기간 을 더합니다.
const [date, term] = privacy.split(" ");
const expireDate = new Date(date);
expireDate.setMonth(expireDate.getMonth() + parseInt(termsObject[term]));
2019-05-31T15:00:00.000Z
2020-02-14T15:00:00.000Z
2020-01-01T15:00:00.000Z
2019-11-30T15:00:00.000Z
2019-03-27T15:00:00.000Z
3. 만료가 된 요소의 번호를 담은 배열을 반환합니다.
if (new Date(today) >= expireDate) {
result.push(index + 1);
}
});
return result;