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

개인정보 수집 일자(privacies)에서 map 을 사용하여,
유효기간이 만료된 데이터만 추출해오려고 했다.

function solution(today, terms, privacies) {  
    const answer = privacies.map((val, idx) => {
        // 유효기간
        const valMonth = new Date(val.split(' ')[0]);
        // 약관 종류
        const valType = val.split(' ')[1];
        
        // 약관 종류에 따른 유효기간
        const addMonth = Number(terms.find((term) => term.includes(valType)).split(' ')[1]);
        // 약관 종류에 따른 유효기간 + 개인정보 수집 일자
        valMonth.setMonth(addMonth + valMonth.getMonth());
        
        // 오늘 날짜와 유효기간 비교 후 번호 리턴
        if(valMonth <= new Date(today)) return idx+1;
    }).filter((val) => val);

    return answer;
}
반응형