문제 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
풀이
function solution(today, terms, privacies) {
let answer = [];
const todayTimestamp = new Date(today.replaceAll('.','-')).getTime();
//terms 배열을 { termKind, duration} 형태로 변환
const termsObj = {};
terms.forEach(term => {
const [termKind, duration] = term.split(' ');
termsObj[termKind] = parseInt(duration);
});
//privacies를 순회하며 유효기간 비교
privacies.forEach((privarcy, idx)=>{
const [date, termKind] = privarcy.split(' ');
const [year, month, day] = date.split('.').map(Number);
//시작 날짜에서 유효기간 추가
const duration = termsObj[termKind];
const expirationDate = new Date(year, month-1+duration, day); //범위 넘어가면 알아서 변환함
//오늘 날짜와 비교
if(expirationDate.getTime() < todayTimestamp) {
answer.push(idx+1);
}
});
return answer;
}
Date() 객체에 대해 많이 배울 수 있었다.
특히 month부분에 13을 넣으면 알아서 다음 년도로 계산한다는 점이 편리했다.
'알고리즘' 카테고리의 다른 글
[프로그래머스] Lv 1. 신규 아이디 추천 - Javascript 풀이 (0) | 2025.01.11 |
---|---|
[프로그래머스] Lv 1. 신고 결과 받기 - Javascript 풀이 (0) | 2025.01.11 |
[프로그래머스] Lv 1. 숫자 문자열과 영단어 - Javascript 풀이 (0) | 2025.01.09 |
[프로그래머스] Lv 1. 성격 유형 검사하기 - Javascript 풀이 (0) | 2025.01.08 |
[프로그래머스] Lv 1. - 가장 많이 받은 선물 : Javascript 풀이 (0) | 2025.01.05 |