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
๋ฐ์ํ