2023/프로그래머스
대소문자 바꿔서 출력하기
옥식
2023. 10. 6. 22:02
3문제를 풀면서 다진 실력으로 이번에는 수월하게 검색했다.
문자열을 대소문자를 바꿔주는 메소드인
toUpperCase() , tolowerCase()를 사용한다.
쉬울 줄 알았는데 생각보다 안된다.
https://velog.io/@rayong/자바스크립트-대소문자-바꿔서-출력하기
자바스크립트, 대소문자 바꿔서 출력하기
제주코딩베이스캠프 자바스크립트 100제를 공부하며 정리한 내용입니다. 문제와 정답 노션 링크(무료), 인프런 해설 강의(유료)문자열이 주어지면 대문자와 소문자를 바꿔서 출력합니다.문자열
velog.io
여기 참고해서 하는 중
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
let swich = [];
rl.on('line', function (line) {
input = [line];
}).on('close',function(){
str = input[0];
for (let i = 0; i<str.length;i++){
if(str[i] === str[i].toUpperCase()){//대문자면 소문자로 바꿔야 함
swich.push(str[i].toLowerCase());
//swich[i] = str[i].toLowerCase();
} else{ //소문자면 대문자로
swich.push(str[i].toUpperCase());
//swich[i] = str[i].toUpperCase();
}
}
console.log(swich);
});
.. 계속 오류도 뜨지 않고 결과값없이 나와서 몇번을 갈아엎다가 이유를 알았따.
for문의 str.length에서 lenth라고 오타가 나는 바람에... 반복문이 전혀 실행되지 않았던 것이다........
내 코드는 틀리지 않았다.
오타가 있었을 뿐이다.
그리고 이대로 출력하면 이건 swich배열 안에 있는 상태이기 때문에 전부 하나하나 떨어져서 나오게 된다.
때문에 join()메서드를 이용하여 배열을 문자열로 합쳐주면 완성된다.
console.log(swich.join(""));
최종 제출
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = [line];
}).on('close',function(){
str = input[0];
let swich = [];
for(let i =0;i<str.length;i++){
if(str[i] === str[i].toUpperCase()){
swich[i] = str[i].toLowerCase();
} else{
swich[i] = str[i].toUpperCase();
}
}
console.log(swich.join(""));
});
다른사람 풀이 중 이게 정말 멋졌다
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = [line];
}).on('close',function(){
str = input[0];
ss = ""; //배열이 아니라 문자열로 선언
for(let i of str){ //for of 반복문 사용
if(i == i.toUpperCase()){
ss += i.toLowerCase();
}
else{
ss += i.toUpperCase();
}
}
console.log(ss)
});