๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ฐœ์ธ์ •๋ณด ์ˆ˜์ง‘ ์œ ํšจ๊ธฐ๊ฐ„

728x90

์–ด.... ์ฒจ์— ์ž‘์„ฑํ•˜๋˜ ์ฝ”๋“œ์—์„œ ๋‚ ์งœ ๊ณ„์‚ฐ์ด ๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ ค์„œ 

๊ณ„์† 2-3๊ฐœ์”ฉ ์ผ€์ด์Šค ์‹คํŒจ๊ฐ€ ๋–ด๋‹ค.

 

function solution(today, termsSt, privacies) {
    const terms = Object.fromEntries(termsSt.map(t =>
        [t.split(" ")[0], Number(t.split(" ")[1])]
    ));
    today = today.split(".").map(Number);
    return privacies.reduce((acc, p, i) => {
        let [y, m, d] = p.split(" ")[0].split(".").map(Number);
        const type = p.split(" ")[1];
        let limitDate = Array(3);
        const sumM = terms[type] + m;
        console.log("sumM", sumM)
        if (sumM < 12) {
            limitDate = [y, sumM, d - 1];
        } else if (sumM === 12) {
            console.log(y, m, d, sumM)
        } else {
            // console.log("sumM", sumM)
            y += Math.floor((sumM) / 12);
            limitDate = d - 1 === 0 ? [y, (sumM) % 12 === 0 ? 12 : (sumM) % 12 - 1, 28] : [y, (sumM) % 12 === 0 ? 12 : (sumM) % 12, d - 1];
            // console.log(today, limitDate)

        }
        console.log(today, limitDate)
        return today[0] > limitDate[0] ?
            [...acc, i + 1]
            : (today[0] === limitDate[0] && today[1] > limitDate[1]) ?
                [...acc, i + 1]
                : (today[0] === limitDate[0] && today[1] === limitDate[1] && today[2] > limitDate[2]) ?
                    [...acc, i + 1] : acc;
    }, [])
}

// console.log(solution("2022.05.19", ["A 6", "B 12", "C 3"], ["2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C"]));
// console.log(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"]))
console.log(solution("2020.12.17", ["A 12"], ["2010.01.01 A", "2019.12.17 A"])) //12
// console.log(solution("2021.01.28", ["A 2"], ["2020.12.01 A", "2010.01.01 A"])) //2

์ด๋ฆฌ์ €๋ฆฌ ํ•ด๋ดค๋Š”๋ฐ ์ ์  ์—‰์ผœ๊ฐ....

 

 

 

 fromEntries ๋Š” ๋‚ด๊ฐ€ ์•„๋Š” ๊ฑฐ์˜ ์œ ์ผํ•œ Object ๋ณ€ํ™˜ ํ•จ์ˆ˜์ธ๋ฐ ๋‹ค๋“ค ์ข‹๋‹ค๊ณ  ํ•˜์‹ฌ. ํ—คํ—ค

 

๊ทธ๋ž˜์„œ ๋ฏผ์šฐ๋‹˜ ์ฝ”๋“œ์—์„œ ๋‚ ์งœ ๊ณ„์‚ฐํ•˜๋Š” ๋ถ€๋ถ„์„ ์Œ”๋น„์ง€ ํ–ˆ๋‹ค. ํ›„ํ›„

function solution(today, termsSt, privacies) {
    const terms = Object.fromEntries(termsSt.map(t => {
        const [k, v] = t.split(" ");
        return [k, Number(v)];
    }
    ));
    const todayDate = new Date(today);

    return privacies.reduce((acc, p, i) => {
        const [pDate, type] = p.split(" ");
        const limitDate = new Date(pDate);
        limitDate.setMonth(limitDate.getMonth() + terms[type]);
        return todayDate >= limitDate ? [...acc, i + 1] : acc;
    }, [])
}

getMonth(), setMonth ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค. !

 

๊ทธ๋ฆฌ๊ณ  terms ๋ฅผ obj ๋กœ ๋งŒ๋“œ๋Š” ๋ถ€๋ถ„์€ ๋น„๋‚œ๋‹˜์ด split ๋‘๋ฒˆ ์“ฐ์ธ ๋ถ€๋ถ„ ๋ฆฌํŽ™ํ† ๋ง ํ•ด๋ณด๋ผ๊ณ  ํ•˜์…”์„œ ๊ณ ์ณค๋‹ค!

ํ›„ Date ํ—ท๊ฐˆ๋ ค

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•