๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/์ธํ”„๋Ÿฐ(inflearn)

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] 12 - ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜

by YWTechIT 2021. 8. 12.
728x90

๐Ÿ“ 12 - ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜

๋Œ€๋ฌธ์ž์ผ๋•Œ ์†Œ๋ฌธ์ž๋กœ ์†Œ๋ฌธ์ž์ผ๋•Œ๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜์‹œํ‚ค๋Š”๋ฐ ASCII ๋ฐฉ๋ฒ•๊ณผ toUpperCase, toLowerCase ๋ฐฉ๋ฒ•์œผ๋กœ ๋‚˜๋ˆ„์–ด ํ’€์—ˆ๋‹ค.

console.log(solution("StuDY"));

function solution(s) {
    let ans = "";

    for(let x of s){
        let num = x.charCodeAt();

        if(num>=65 && num<=91) ans+=String.fromCharCode(num+32);
        else ans+=String.fromCharCode(num-32);
    }
  return ans;
}

function solution(s) {
    let ans = "";

    for(let x of s){
        if (x === x.toUpperCase()) ans+=x.toLowerCase();
        else ans += x.toUpperCase();
    }
  return ans;
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€