https://school.programmers.co.kr/learn/courses/30/lessons/12901
์ค๋์ ๋ฌธ์ ๋ 2016๋ ์ ์,์ผ์ด ์ ํด์ก์ ๋ ์์ผ์ ๊ตฌํ๋ ๋ฌธ์ .
๋์ ๋๊ธธ์ ์ฌ๋ก์ก์ '์ค์ ๋ก ์๋ ๋ ์ ๋๋ค'
์ฅ ๊ทธ๋ผ ์ง์ง ๋ฌ๋ ฅ์ด๋ ๋๊ฐ๋ค?
๊ท์ฐฎ๊ฒ ์ค๋ ๋ฑ๋ฑ ๊ณ์ฐํ ํ์๊ฐ ์๊ฒ ๊ตฐ ์ด๋ผ๊ณ ์๊ฐ์ด ๋ค์์.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString
MDN ๊ฐ์ ์์ผ์ ๋ฐํํด์ฃผ๋ ๋ฉ์๋๋ฅผ ์ฐพ์๋ค.
const event = new Date(1993, 6, 28, 14, 39, 7);
console.log(event.toString());
// Expected output: "Wed Jul 28 1993 14:39:07 GMT+0200 (CEST)"
// Note: your timezone may vary
console.log(event.toDateString());
// Expected output: "Wed Jul 28 1993"
์ด๋ฐ ์์ผ๋ก ์์ผ, ์ ๊น์ง๋ ์์ด๋ก ๋์ด.
function solution(a, b) {
const date = new Date(2016, a-1, b);
return date.toDateString().slice(0, 3).toUpperCase()
}
๊ทธ๋์ ๊ฐ๋จํ๊ฒ ์์ผ ๊น์ง๋ง ์๋ผ๋ด๊ณ , UPPER CASE ๋ฅผ ์ ์ฉํ๋ค.
์ฌ์ค ์ฝ๊ฐ ๊ผผ์๊ฐ์ ๋๋์ด ๋ค๊ธด ํจ. ๋ฐํท..
์๋๋ ์ฐ๋ฆฌ ์คํฐ๋ ๋ฉค๋ฒ์ธ ๋ด๋์ ์ฝ๋.
function solution(a, b) {
const date = [31,29,31,30,31,30,31,31,30,31,30,31];
const day = ["THU","FRI","SAT","SUN","MON","TUE","WED"];
const sumDate = date.reduce((acc,curr,idx)=>
idx < a-1 ? acc+curr : acc ,b) ;
return day[sumDate % day.length ];
}
date ๋ก ๋ ์ง์ ์๋ฅผ ๋๊ณ , day ๋ก ์์ผ์ ๋ฐฐ์ด๋ก ๋ง๋ค์๋ค.
์๊ณผ index ๋ฅผ ๋น๊ตํ๋ฉฐ, index ๊ฐ ๋ ์์ ๊ฒฝ์ฐ date ์ ์ซ์๋ฅผ ๋ํด๊ฐ๋ค. => ์ด ๋ ์ง ์๊ฐ ๋์ด.
์ด ๋ ์ง ์๋ฅผ ์์ผ๋ก ๋๋ ๋๋จธ์ง๋ 1์1์ผ ๋ถํฐ ์์ธ ๋ ์ง์ ์์ผ index ๊ฐ ๋๋ค.
๊ทธ๋ผ ์์ผ์ ๊ตฌํ ์ ์์!
? ์์๊น? ๊ถ๊ธํด์ ์ฐพ์๋ด.
https://stackoverflow.com/questions/36144406/how-to-early-break-reduce-method
์ค์ฐ ๋ฐฐ์ด ์์ฒด๋ฅผ ๊ฑด๋ค๋ฉด ๋๋ ๊ฑฐ์๋ค.
์ ๊ธฐ~~
https://hwani.dev/js-reduce-break/
ํ๊ธ๋ก ์จ์๋ ๋ธ๋ก๊ทธ๋ ์ฐพ์.
function solution2(a, b) {
const date = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const day = ["THU", "FRI", "SAT", "SUN", "MON", "TUE", "WED"];
const sumDate = date.reduce((acc, curr, idx, arr) => {
console.log(acc, idx)
if (idx < a-1) {
return acc + curr;
} else {
arr.splice(1);
return acc;
}
}, b);
console.log(day[sumDate % day.length]);
return day[sumDate % day.length];
}
๋ด๋์ ์ฝ๋์ splice ์ผ๋ก ๋ฐฐ์ด์ ๊ฑด๋ค๋ฉด,
์์ ์๋ ๊ธฐ์กด ์ฝ๋ ๋ณด๋ค ์๋ splice ๋ฅผ ๋ฃ์ด์ค ์ฝ๋๊ฐ ํ์คํ ๋ ๋๋ ๊ฑธ ํ์ธํ ์ ์์๋ค.
์ฌ๋ฐ๋ค~~
๊ฐ์ด ๊ณต๋ถํ๋ ๋ชฐ๋๋ ๊ฒ๋ ์์๊ฐ๊ณ ์ข๋ค.