Algorithm/Programmers
[알고리즘/프로그래머스/카카오] 개인정보 수집 유효기간(JS)
개발자 김비숑
2023. 11. 16. 08:50
반응형
문제
https://school.programmers.co.kr/learn/courses/30/lessons/150370
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
function solution(today, terms, privacies) {
const termsObj = terms.reduce((acc, cur) => {
const [type, period] = cur.split(' ')
acc[type] = +period
return acc
}, {})
const tDate = new Date(today)
const expiredPrivacies = privacies.map((p, i) => {
const [date, type] = p.split(' ')
const pDate = new Date(date)
pDate.setMonth(pDate.getMonth() + termsObj[type])
// 오늘 기준으로 유효기간이 지난 경우
return pDate <= tDate? i+1 : undefined
}).filter(p => p !== undefined)
return expiredPrivacies
}
Date에서 날짜를 더할 때는 getYear, getMonth, getDate를 활용해 필요한 연, 월, 일을 더해준다.
그냥 숫자를 더해주는 경우, 자바스크립트는 Date 객체를 milliseconds으로 변환하여 해당 숫자를 더하게 된다.
문제는 간단했는데 오랜만에 Date 객체를 써서 숫자를 더하면 일자를 더한다고 생각하는 실수를 했다. Date 객체에 대해 복습하는 시간을 가져야겠다.
let currentDate = new Date();
let result = currentDate + 10; // currentTime을 milliseconds으로 변환해 + 10
다른 사람들의 풀이를 보니 Date 객체를 사용하지 않고, 아예 milliseconds으로 변환해 푸는 방법도 있었다. 이 방법도 따로 메서드를 사용하지 않아도 되니 좋은 것 같다.
반응형