Algorithm/μΈνλ°(inflearn)
[JavaScript/section 1] 13 - λμλ¬Έμ λ³ν
_μ±νΈ_
2022. 9. 3. 10:59
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'));