프로그래머스 LEVEL 1(개인정보 수집 유효기간)

image

  • 사용 언어 : javascript

  • 해결 날짜 : 2023-01-09

  • 해결 방법 :

    • terms를 돌며 map에 해당 약관 종류 별 유효기간 저장
    • privacies를 돌며 Date로 변환한 각 날짜에 약관 종류 별 유효기간 추가
      • 이 때 Date 객체에 setMonth()를 통해 추가
    • Date로 변환한 today와 비교하여 크거나 같다면 answer에 추가
  • 회고 :

    • Date 객체의 setMonth(), getMonth() 활용
  • 코드

    function solution(today, terms, privacies) {
      const answer = [];
      const map = new Map();
    
      terms.forEach((v) => {
        const [kinds, exp] = v.split(' ');
        map.set(kinds, parseInt(exp, 10));
      });
    
      privacies.forEach((v, i) => {
        const [date, kinds] = v.split(' ');
        const expDate = new Date(date);
        expDate.setMonth(expDate.getMonth() + map.get(kinds));
    
        if (new Date(today) >= expDate) answer.push(i + 1);
      });
    
      return answer;
    }
    
  • 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges