728x90
๐ 08 - A๋ฅผ #์ผ๋ก
A
๋ฅผ ๋ชจ๋ #
์ผ๋ก ๋ฐ๊พธ๊ธฐ๋ง ํ๋ฉด ๋๋๋ฐ, 3๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ํ์๋ค.
split()
+map()
for - of
replace
+reg
๋ง์ง๋ง์ ์ ๊ท์์ /A/g
๋ ๋๋ฌธ์ A
๋ฅผ ๋ชจ๋(g
) #
์ผ๋ก ๋ฐ๊ฟ๋ฌ๋ผ๋ ์๋ฏธ๋ค.
console.log(solution("BANANA"));
// 1. split() + map()
function solution(s){
s = s.split("").map((item) => item === "A" ? "#" : item)
return s.join("");
}
// 2. for - of
function solution(s){
let answer = "";
for (let i of s){
if (i === "A") answer += "#"
else answer += i
}
return answer;
}
// 3. replace + reg
function solution(s){
let answer = s;
answer = answer.replace(/A/g, "#");
return answer;
}
๋ฐ์ํ
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 11 - ๋๋ฌธ์๋ก ํต์ผ (0) | 2021.08.12 |
---|---|
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 10 - ๋ฌธ์ ์ฐพ๊ธฐ (0) | 2021.08.11 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 08 - ์ผ๊ณฑ ๋์์ด (0) | 2021.08.11 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 07 - 10๋ถ์ (0) | 2021.08.11 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 06 - ํ์ (0) | 2021.08.11 |
๋๊ธ