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

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/Javascript] [level.0] ํŠน์ • ๋ฌธ์ž ์ œ๊ฑฐํ•˜๊ธฐ || ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์ž…๋ฌธ

728x90

๐Ÿšฅ replace() ์™€ for๋ฌธ ์ด์šฉ

function solution(my_string, letter) {
    var answer = "";
    for (let i = 0; i < my_string.length; i++) {
        my_string = my_string.replace(letter, "");
    }
    answer = my_string;
    return answer;
}

replace ๋งŒ ์•Œ์•„์„œ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋‹ค. 

 

 

๐Ÿšฅ replaceAll()

ํ•˜์ง€๋งŒ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜์—ˆ๋‹ค 

replaceAll ์ด๋ž€๊ฒŒ ์žˆ์—ˆ์Œ!

function solution(my_string, letter) {
    return my_string.replaceAll(letter, "");
}

์–˜๊ฐ€ ํ™œ์šฉ๋„๊ฐ€ ํ›จ์”ฌ ๋†’์„ ๊ฒƒ ๊ฐ™๋‹ค.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

 

String.prototype.replaceAll() - JavaScript | MDN

replaceAll() ๋ฉ”์„œ๋“œ๋Š” pattern์˜ ๋ชจ๋“  ์ผ์น˜ ํ•ญ๋ชฉ์ด replacement๋กœ ๋Œ€์ฒด๋œ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. pattern์€ ๋ฌธ์ž์—ด ๋˜๋Š” RegExp์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ replacement๋Š” ๊ฐ ์ผ์น˜ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ํ˜ธ์ถœ๋˜๋Š” ๋ฌธ์ž์—ด ๋˜๋Š” ํ•จ์ˆ˜์ผ

developer.mozilla.org

 

 

 

๐Ÿšฅsplite ์™€ join('')์„ ์ด์šฉ

๊ทผ๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ ๋ณด๊ธฐ๋กœ ์žฌ๋ฐŒ๋Š” ๋ฐฉ๋ฒ•๋„ ์•Œ์•˜๋‹ค!

function solution(my_string, letter) {
    const answer = my_string.split(letter).join('')
    return answer;}

์ œ๊ฑฐํ•ด์•ผํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์ ์œผ๋กœ ์ชผ๊ฐ  ํ›„, ๋‹ค์‹œ ๋ถ™์ด๋Š” ๋ฐฉ๋ฒ•!

console.log("abcdef".split("f"));
//[ 'abcde', '' ]


console.log(['abcde', ''].join(""));
//abcde

join์€ ์–ด๋–ค๊ฑธ๋กœ ๋ถ™์ผ์ง€๋ฅผ () ์•ˆ์— ์จ์ค˜์•ผํ•œ๋‹ค.

์•„๋ฌด๊ฒƒ๋„ ์จ์ฃผ์ง€ ์•Š์œผ๋ฉด , ๊ฐ€ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/join

 

Array.prototype.join() - JavaScript | MDN

join() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•ด ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

developer.mozilla.org

 

728x90
๋ฐ˜์‘ํ˜•