[JavaScript/section 1] 16 - ์ค‘๋ณต๋ฌธ์ž์ œ๊ฑฐ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ์†Œ๋ฌธ์ž๋กœ ๋œ ํ•œ๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๋ฉด ์ค‘๋ณต๋œ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ œ๊ฑฐ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋Š” ์›๋ž˜ ๋ฌธ์ž์—ด์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•œ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋“ค์„ ๋น„๊ตํ•˜๋ฉด์„œ ํ’€์ดํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ Set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๐Ÿ“ ํ’€์ด // includes() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer = ''; for (let x of s) { if (!answer.includes(x)) answer += x; } return answer; } console.log(solution('ksekkset')); // indexOf() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer = ''; for (let..
[JavaScript/section 1] 15 - ๊ฐ€์šด๋ฐ ๋ฌธ์ž ์ถœ๋ ฅ
ยท
Algorithm/์ธํ”„๋Ÿฐ(inflearn)
๐Ÿ“Œ ๋ฌธ์ œ ์†Œ๋ฌธ์ž๋กœ ๋œ ๋‹จ์–ด(๋ฌธ์ž์—ด)๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๊ทธ ๋‹จ์–ด์˜ ๊ฐ€์šด๋ฐ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‹จ, ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ์ง์ˆ˜์ผ ๊ฒฝ์šฐ ๊ฐ€์šด๋ฐ 2๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋‚˜๋Š” ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ ‘๊ทผํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€์ง€๋งŒ substring() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ๋” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์ด์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ•œ๋‹ค. ๐Ÿ“ ํ’€์ด // ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ง์ ‘ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ function solution(s) { let answer; const mid = Math.floor(s.length / 2); if (s.length % 2 === 0) answer = str[mid - 1] + str[mid]; else answer = str[mid]; return answer; } console.log(so..
[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/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'..
_์„ฑํ˜ธ_
'Inflearn' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก (10 Page)