728x90
๋ฐ์ํ
๐ ๋ฌธ์
๋๋ฌธ์์ ์๋ฌธ์๊ฐ ๊ฐ์ด ์กด์ฌํ๋ ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ์ ๋๋ฌธ์๋ ์๋ฌธ์๋ก ์๋ฌธ์๋ ๋๋ฌธ์๋ก ๋ณํํ์ฌ ์ถ๋ ฅํ๋ ๋ฌธ์ ์ด๋ค.
toUpperCase() ๋ฉ์๋์ toLowerCase() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ๋จํ๊ฒ ๋ฌธ์ ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ค.
for...of ๋ช ๋ น๋ฌธ์ ์ฌ์ฉํ์ฌ ๋น๋น ๋๋ฉด์ ์๋ฌธ์์ธ ๊ฒฝ์ฐ toUpperCase() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋๋ฌธ์๋ก ๋ณํํ ํ answer ๋ณ์์ ๋์ ์์ผ์ฃผ๊ณ , ๋๋ฌธ์์ธ ๊ฒฝ์ฐ toLowerCase() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฌธ์๋ก ๋ณํํ ํ answer ๋ณ์์ ๋์ ์์ผ์ฃผ๋ฉด ๋๋ค.
๐ ํ์ด
// toUpperCase() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ๋จ
function solution(s) {
let answer = '';
for (let x of s) {
if (x === x.toUpperCase()) answer += x.toLowerCase();
else answer += x.toUpperCase();
}
return answer;
}
console.log(solution('StuDY'));
// charCodeAt() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ํ๋จ
function solution(str) {
let answer = '';
for (let x of str) {
if (x.charCodeAt() < 91) answer += x.toLowerCase();
else answer += x.toUpperCase();
}
return answer;
}
console.log(solution('StuDY'));
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript/section 1] 15 - ๊ฐ์ด๋ฐ ๋ฌธ์ ์ถ๋ ฅ (0) | 2022.09.05 |
---|---|
[JavaScript/section 1] 14 - ๊ฐ์ฅ ๊ธด ๋ฌธ์์ด (0) | 2022.09.04 |
[JavaScript/section 1] 12 - ๋๋ฌธ์๋ก ํต์ผ (0) | 2022.09.02 |
[JavaScript/section 1] 11 - ๋๋ฌธ์ ์ฐพ๊ธฐ (0) | 2022.09.02 |
[JavaScript/section 1] 10 - ๋ฌธ์ ์ฐพ๊ธฐ (0) | 2022.09.02 |