[JavaScript/section 1] 14 - ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ N๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ๊ทธ ์ค‘ ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด์•ผ ํ•  ๊ฒฝ์šฐ Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER(์•ˆ์ „ํ•œ ์ƒ์ˆ˜)๋ฅผ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ์ข‹๋‹ค. ๐Ÿ“ ํ’€์ด function solution(s) { let answer = '', max = Number.MIN_SAFE_INTEGER; for (let x of s) { if (max < x.length) { max = x.length; answer = x; } } return answer; } let str = ['teacher', 'time', 'student', 'beautiful', 'good']; console.log(solution(str)); function ..
[JavaScript/section 1] 13 - ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๊ฐ™์ด ์กด์žฌํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ์†Œ๋ฌธ์ž๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. toUpperCase() ๋ฉ”์„œ๋“œ์™€ toLowerCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. for...of ๋ช…๋ น๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋น™๋น™ ๋Œ๋ฉด์„œ ์†Œ๋ฌธ์ž์ธ ๊ฒฝ์šฐ toUpperCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ answer ๋ณ€์ˆ˜์— ๋ˆ„์ ์‹œ์ผœ์ฃผ๊ณ , ๋Œ€๋ฌธ์ž์ธ ๊ฒฝ์šฐ toLowerCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ answer ๋ณ€์ˆ˜์— ๋ˆ„์ ์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค. ๐Ÿ“ ํ’€์ด // toUpperCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒ๋‹จ function solution(s) { let answer = ''; for (let x of s) { if (x === x.toUpperCa..
[JavaScript/section 1] 12 - ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๊ฐ€ ๊ฐ™์ด ์กด์žฌํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๋Œ€๋ฌธ์ž๋กœ ๋ชจ๋‘ ํ†ต์ผํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‚˜๋Š” ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ toUpperCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผํ•ด์ฃผ์—ˆ๋‹ค. ๋˜ํ•œ, ๋Œ€๋ฌธ์ž ์ฐพ๊ธฐ ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ charCodeAt() ๋ฉ”์„œ๋“œ์™€, ์•„์Šคํ‚ค์ฝ”๋“œ ๋ฒˆํ˜ธ๋ฅผ ๋ฐ›์•„ ๋ฌธ์ž์—ด์„ ๊ตฌ์„ฑํ•ด์ฃผ๋Š” String.fromCharCode() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•๋„ ์•Œ์•„๋ณด์•˜๋‹ค. ๐Ÿ“ ํ’€์ด // toUpperCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer = ''; answer = s.toUpperCase(); return answer; } let str = 'ItisTimeToStudy'; console.log(solution(str)); // ch..
[JavaScript/section 1] 11 - ๋Œ€๋ฌธ์ž ์ฐพ๊ธฐ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ๋ฌธ์ž์—ด์— ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‚˜๋Š” charCodeAt() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž์˜ ์•„์Šคํ‚ค์ฝ”๋“œ ๋ฒˆํ˜ธ๊ฐ€ 90๊นŒ์ง€๋ผ๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ณด๋‹ค toUpperCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋ผ๊ณ  ํ•œ๋‹ค. ๐Ÿ“ ํ’€์ด // charCodeAt() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer = 0; for (let x of s) { if (x.charCodeAt() < 91) answer++; } return answer; } let str = 'KoreaTimeGood'; console.log(solution(str)); // toUpperCase() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ..
[JavaScript/section 1] 10 - ๋ฌธ์ž ์ฐพ๊ธฐ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ํ•œ ๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๊ณ , ํŠน์ • ๋ฌธ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ํŠน์ •๋ฌธ์ž๊ฐ€ ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์— ๋ช‡ ๊ฐœ ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ๋ฌธ์ œ์ด๋‹ค. (๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.) ๋‚˜๋Š” for...of ๋ช…๋ น๋ฌธ๊ณผ if ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ๋˜ํ•œ, split() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ํ›„ ๋ฐฐ์—ด์˜ ๊ธธ์ด(length)๋ฅผ ๊ตฌํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๐Ÿ“ ํ’€์ด // for...of ๋ช…๋ น๋ฌธ๊ณผ if ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s, t) { let answer = 0; for (let x of s) { if (x === t) answer++; } return answer; } let str = 'COMPUTERPROGRAMMING'; console.log(solution(str, 'R'..
[JavaScript] ํด๋กœ์ €๋ž€ ๋ฌด์—‡์ธ๊ฐ€?
ยท
Frontend/JavaScript
ํด๋กœ์ € โ“ ํ•จ์ˆ˜์™€ ํ•จ์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ์–ดํœ˜์  ํ™˜๊ฒฝ์˜ ์กฐํ•ฉ ์ด ํ™˜๊ฒฝ์€ ํด๋กœ์ €๊ฐ€ ์ƒ์„ฑ๋œ ์‹œ์ ์˜ ์œ ํšจ ๋ฒ”์œ„ ๋‚ด์— ์žˆ๋Š” ๋ชจ๋“  ์ง€์—ญ ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ์ฆ‰, ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ ์™ธ๋ถ€ ํ•จ์ˆ˜์— ์žˆ๋Š” ์ƒํƒœ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์™œ ์‚ฌ์šฉํ• ๊นŒ โ“ ๋‚ด๋ถ€ ์ •๋ณด๋ฅผ ์€๋‹‰ํ•˜๊ณ , ๊ณต๊ฐœ ํ•จ์ˆ˜(public ๋˜๋Š” ์™ธ๋ถ€)๋ฅผ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ์กฐ์ž‘์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. (์บก์Šํ™”, ์ •๋ณด์€๋‹‰) ์ „์—ญ๋ณ€์ˆ˜ ๋˜ํ•œ ์ค„์—ฌ์ง€๊ฒŒ ๋˜๋ฏ€๋กœ ๊ฐœ๋ฐœ ๊ณผ์ •์—์„œ ์‹ค์ˆ˜๋กœ ์ ‘๊ทผํ•˜๋Š” ์ผ์ด ์•ˆ์ƒ๊ธด๋‹ค. ํ˜„์žฌ๋Š” ํด๋ž˜์Šค์—์„œ์˜ private ํ•„๋“œ ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์–ด๋–ป๊ฒŒ ์ด๊ฒƒ์ด ๊ฐ€๋Šฅํ• ๊นŒ โ“ inner ๋ ‰์‹œ์ปฌ ํ™˜๊ฒฝ์ด ์™ธ๋ถ€ ๋ ‰์‹œ์ปฌ ํ™˜๊ฒฝ์„ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ํด๋กœ์ €๋ฅผ ์ž˜ ์ดํ•ดํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ณ€์ˆ˜์˜ ์œ ํšจ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๋Š”์ง€(Lexical scoping)๋ฅผ ๋จผ์ € ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค...
[JavaScript/section 1] 09 - A๋ฅผ #์œผ๋กœ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ๋Œ€๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ์˜์–ด ๋‹จ์–ด๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๋‹จ์–ด์— ํฌํ•จ๋œ 'A'๋ฅผ ๋ชจ๋‘ '#'์œผ๋กœ ๋ฐ”๊พธ์–ด ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‚˜๋Š” ๋ฌธ์ž์—ด์„ split() ๊ณผ map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ๋˜ํ•œ, for...of ๋ช…๋ น๋ฌธ๊ณผ replace() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค. ๐Ÿ“ ํ’€์ด // split(), map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer = ''; answer = s .split('') .map((x) => { if (x === 'A') { return '#'; } return x; }) .join(''); return answer; } let str = 'BANANA'..
[JavaScript/section 1] 08 - ์ผ๊ณฑ ๋‚œ์Ÿ์ด
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ์™•๋น„๋ฅผ ํ”ผํ•ด ์ผ๊ณฑ ๋‚œ์Ÿ์ด๋“ค๊ณผ ํ•จ๊ป˜ ํ‰ํ™”๋กญ๊ฒŒ ์ƒํ™œํ•˜๊ณ  ์žˆ๋˜ ๋ฐฑ์„ค๊ณต์ฃผ์—๊ฒŒ ์œ„๊ธฐ๊ฐ€ ์ฐพ์•„์™”๋‹ค. ์ผ๊ณผ๋ฅผ ๋งˆ์น˜๊ณ  ๋Œ์•„์˜จ ๋‚œ์Ÿ์ด๊ฐ€ ์ผ๊ณฑ ๋ช…์ด ์•„๋‹Œ ์•„ํ™‰ ๋ช…์ด์—ˆ๋˜ ๊ฒƒ์ด๋‹ค. ์•„ํ™‰ ๋ช…์˜ ๋‚œ์Ÿ์ด๋Š” ๋ชจ๋‘ ์ž์‹ ์ด "๋ฐฑ์„ค ๊ณต์ฃผ์™€ ์ผ๊ณฑ ๋‚œ์Ÿ์ด"์˜ ์ฃผ์ธ๊ณต์ด๋ผ๊ณ  ์ฃผ์žฅํ–ˆ๋‹ค. ๋›ฐ์–ด๋‚œ ์ˆ˜ํ•™์  ์ง๊ด€๋ ฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๋ฐฑ์„ค๊ณต์ฃผ๋Š”, ์ผ๊ณฑ ๋‚œ์Ÿ์ด์˜ ํ‚ค์˜ ํ•ฉ์ด 100์ด ๋จ์„ ๊ธฐ์–ตํ•ด ๋ƒˆ๋‹ค. ์•„ํ™‰ ๋‚œ์Ÿ์ด์˜ ํ‚ค๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฐฑ์„ค๊ณต์ฃผ๋ฅผ ๋„์™€ ์ผ๊ณฑ ๋‚œ์Ÿ์ด๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ด์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ 2๊ฐœ์˜ ์ž์—ฐ์ˆ˜๋ฅผ ๋ฝ‘์€ ํ›„ 9๊ฐœ์˜ ์ž์—ฐ์ˆ˜ ํ•ฉ(sum)์—์„œ ๋บ€ ๊ฐ’์ด 100์ด ๋˜์–ด์•ผ ํ•œ๋‹ค. ๊ฐ•์˜๋Œ€๋กœ splice() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ filter() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ์›์†Œ๋“ค๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋‹ค..
_์„ฑํ˜ธ_
๐ŸŒฑ ์„ฑํ˜ธ ๋ธ”๋กœ๊ทธ