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

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

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 2016๋…„ / reduce break

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/12901

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

์˜ค๋Š˜์˜ ๋ฌธ์ œ๋Š” 2016๋…„์˜ ์›”,์ผ์ด ์ •ํ•ด์กŒ์„ ๋•Œ ์š”์ผ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.

 

๋‚˜์˜ ๋ˆˆ๊ธธ์„ ์‚ฌ๋กœ์žก์€ '์‹ค์ œ๋กœ ์žˆ๋Š” ๋‚ ์ž…๋‹ˆ๋‹ค'

 

์—ฅ ๊ทธ๋Ÿผ ์ง„์งœ ๋‹ฌ๋ ฅ์ด๋ž‘ ๋˜‘๊ฐ™๋„ค?

๊ท€์ฐฎ๊ฒŒ ์œค๋…„ ๋“ฑ๋“ฑ ๊ณ„์‚ฐํ•  ํ•„์š”๊ฐ€ ์—†๊ฒ ๊ตฐ ์ด๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ์Œ.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

 

Date.prototype.toDateString() - JavaScript | MDN

toDateString() ๋ฉ”์„œ๋“œ๋Š” ๋ฏธ๊ตญ ์˜์–ด๋กœ ์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜์žˆ๋Š” ํ˜•ํƒœ๋กœ Date ๊ฐ์ฒด์˜ ๋‚ ์งœ ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

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

 

How to early break reduce() method?

How can I break the iteration of reduce() method? for: for (var i = Things.length - 1; i >= 0; i--) { if(Things[i] <= 0){ break; } }; reduce() Things.reduce(function(memo, current){ ...

stackoverflow.com

 

์˜ค์šฐ ๋ฐฐ์—ด ์ž์ฒด๋ฅผ ๊ฑด๋“ค๋ฉด ๋˜๋Š” ๊ฑฐ์˜€๋‹ค.

์‹ ๊ธฐ~~

 

https://hwani.dev/js-reduce-break/

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ reduce break

์„œ๋ก  Javascript ๋ฉ”์„œ๋“œ๋ž€ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•œ ์ฃผ์–ด์ง„ ๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‚˜์—๊ฒŒ๋Š” ์• ์ฆ์˜ ๊ด€๊ณ„์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•œ ๊ฒƒ ๊ฐ™์•„์„œ ๊ธฐํ”ผํ•˜๋‹ค๊ฐ€๋„, ์‹ค์ œ ์‚ฌ์šฉ

hwani.dev

 

ํ•œ๊ธ€๋กœ ์จ์žˆ๋Š” ๋ธ”๋กœ๊ทธ๋„ ์ฐพ์Œ. 

 

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 ๋ฅผ ๋„ฃ์–ด์ค€ ์ฝ”๋“œ๊ฐ€ ํ™•์‹คํžˆ ๋œ ๋„๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

 

์žฌ๋ฐŒ๋‹ค~~

๊ฐ™์ด ๊ณต๋ถ€ํ•˜๋‹ˆ ๋ชฐ๋ž๋˜ ๊ฒƒ๋„ ์•Œ์•„๊ฐ€๊ณ  ์ข‹๋‹ค.

728x90
๋ฐ˜์‘ํ˜•