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
๐ฅ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
728x90
๋ฐ์ํ