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

ํ”„์—” ๊ณต๋ถ€

(151)
[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฆฌ์ŠคํŠธ ์ž๋ฅด๊ธฐ ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ ๋‚˜์„œ ์ด๊ฑฐ์Šจ switch ๋ฌธ์„ ์“ฐ๊ธฐ ๋”ฑ ์ข‹๋‹ค! ๋ผ๊ณ  ์ƒ๊ฐํ•จ. function solution(n, slicer, num_list) { let result = []; switch (n) { case 1: result = num_list.slice(0, slicer[1] + 1); break; case 2: result = num_list.slice(slicer[0]); break; case 3: result = num_list.slice(slicer[0], slicer[1] + 1); break; case 4: result = num_list.slice(slicer[0], slicer[1] + 1).filter((v, i) => i % slicer[2] === 0); } return(result..
[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฌธ์ž์—ด ๋ฌถ๊ธฐ function solution(strArr) { let lengthArr = new Array(30).fill(0); strArr.map(v => lengthArr[v.length - 1] += 1) return Math.max(...lengthArr); } ์ฃผ์–ด์ง€๋Š” ์›์†Œ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€๊ฐ€ 30์ด๊ธฐ ๋•Œ๋ฌธ์— 30๊ฐœ์˜ ์ž๋ฆฌ๋ฅผ ๊ฐ€์ง„ ๋ฐฐ์—ด์„ ๋จผ์ € ๋งŒ๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฃผ์–ด์ง„ strarr ์˜ ์›์†Œ์˜ ๊ธธ์ด-1 ํ•œ index ์— +1์„ ํ•ด์ค€๋‹ค. ์ฒ˜์Œ์— fill(0) ์œผ๋กœ ์ˆซ์ž๋ฅผ ๋„ฃ์–ด์ฃผ์ง€ ์•Š์œผ๋ฉด undefined ์— +1 ์„ ํ•ด์ฃผ๋Š” ๊ฑฐ๋‚˜ ๋งˆ์ฐฌ๊ฐ€์ง€๋ผ์„œ ๊ผญ 0์„ ๋„ฃ์–ด์ค€๋‹ค. map ์—ฐ์‚ฐ์ด ๋๋‚œ ํ›„ lengthArr ์—์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ return ํ•ด์ค€๋‹ค.
[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ปคํ”ผ ์‹ฌ๋ถ€๋ฆ„ ๋ผ๋–ผ๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š์€ ๋ฌธ์ž์—ด์˜ ๊ฒฝ์šฐ์—” ์ „๋ถ€ 4500์›์ด๋‹ˆ, cafelatte ํ•˜๋Š” ๋‹จ์–ด๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ณ  ๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. function solution(order) { return order.reduce((acc, choice) => choice.includes("cafelatte") ? acc + 5000 : acc + 4500, 0); }
[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ชจ์˜๊ณ ์‚ฌ https://school.programmers.co.kr/learn/courses/30/lessons/42840 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr function solution(answers) { const a = [1, 2, 3, 4, 5]; const b = [2, 1, 2, 3, 2, 4, 2, 5]; const c = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]; const checkFn = (pattern, answers) => { let test = [...answers]; const checkCount = test...
[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ณผ์ผ ์žฅ์ˆ˜ ๋ฐฐ์—ด์€ index ๋งŒ ์ž˜ ์ด์šฉํ•ด๋„!! ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ํš๊ธฐ์ ์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค!!! function solution(k, m, score) { score.sort((a, b) => b - a); let sum = 0; for (let i = 0; i index ๊ฐ€ ๊ทœ์น™์ ์ž„. ์ด๋ผ๋Š” ํžŒํŠธ๋ฅผ ์–ป์–ด ํ†ต๊ณผํ–ˆ๋‹ค. ์˜ฌ๋ฆผ ์ฐจ์ˆ˜๋กœ ์ •๋ ฌํ•ด์ค€ ๋ฐฐ์—ด์„ 4๊ฐœ์”ฉ ์ž๋ฅด๋ฉด index๊ฐ€ 4-1, 8-1... ์ด๋ ‡๊ฒŒ ๋ฐ”๋กœ ์ „ index ๋งŒ ํ•„์š”ํ•˜๊ฒŒ ๋œ๋‹ค. ํ•ด๋‹นํ•˜๋Š” index *m..
[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] 2016๋…„ / reduce break 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.prot..
[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฝœ๋ผ๋ฌธ์ œ ๊ฐœ์ธ์ ์œผ๋กœ ์ฝœ๋ผ๋ฌธ์ œ ๊ฐ™์€ ๋ฌธ์ œ ์œ ํ˜•์ด ์‹ซ๋‹ค....... function solution(a, b, n) { let count = 0; while (n >= a) { count += Math.floor(n / a) * b; n = n % a + Math.floor(n / a) * b; } return count; } ์‚ฌ์‹ค ๊ตฌํ•˜๊ณ  ๋‚˜๋ฉด ๋ณ„๊ฑฐ ์—†์ง€๋งŒ, ๋ฌธ์ œ ๋ถ€ํ„ฐ ๋ณต์žกํ•˜๊ฒŒ ๋Š๊ปด์ง. ์•„๋ž˜๋Š” ์Šคํ„ฐ๋”” ํŒ€์›๋ถ„์˜ ํ’€์ด. ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ ํ’€์ด๋‹ค. ์žฌ๊ท€ํ•จ์ˆ˜ ๊ฑฐ์˜ ์•ˆ์จ๋ด์„œ ๋ณผ ๋•Œ๋งˆ๋‹ค ์‹ ๊ธฐํ•˜๋‹ค.... ๊ทผ๋ฐ while ๋ฌธ์˜ ๋ฌดํ•œ๋ฃจํ”„์˜ ์œ„ํ—˜์—์„œ ๋ฒ—์–ด๋‚˜๊ธฐ์— ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™๊ธฐ๋‘ ํ•˜๋‹ค! function solution(a, b, n) { function helper(b,n,a){ if((a / b) < 1) return 0; const..
[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฌธ์ž์—ด์ด ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”์ง€ ์„ธ๊ธฐ - reduce ํ™œ์šฉ๋ฒ• ์ด ๋ฌธ์ œ๋Š” ํ’€์—ˆ๋‹ค๊ธฐ ๋ณด๋‹จ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ๊ณต๋ถ€ํ–ˆ๋‹ค..! reduce ๋ฅผ ์ด์šฉํ•ด ์„ธ์•ผํ•˜๋Š” ๊ฐ’๋งŒ count ํ•ด์ค€๋‹ค. ์„ธ์•ผ ํ•˜๋Š” ๊ฐ’์€ conStr ๊ณผ pat ์„ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค. ๋น„๊ตํ•  conStr ์€ ๊ธฐ์กด ๋ฌธ์ž๋ฅผ pat ์˜ ๊ธธ์ด๋งŒํผ์”ฉ ์ž˜๋ผ๋‚ธ ๊ฐ’์ธ๋ฐ, ์„ค๋ช…์ฒ˜๋Ÿผ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— ์ฃผ์–ด์ง„ myString ์—๋Š” ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค. function solution(myString, pat) { const result = [...myString].reduce((acc, cur, ind) => { const conStr = myString.slice(ind, pat.length + ind) console.log(acc, cur, ind) return conStr === pat ? acc + 1..

728x90
๋ฐ˜์‘ํ˜•