배움 기록/코테 연습
[프로그래머스] 대소문자 바꿔서 출력하기 : JavaScript
dygreen
2023. 11. 7. 15:21
📌 문제
📌 풀이
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 arr = [];
for (let i = 0; i < str.length; i++) {
if (str[i] === str[i].toUpperCase()) {
arr.push(str[i].toLowerCase());
} else {
arr.push(str[i].toUpperCase());
}
}
console.log(arr.join(""));
});
- if 문에서는
input
의 글자가 대문자이면, 소문자로 바꿔서 arr 배열 안으로 집어넣는다.
else 문에서는input
의 글자가 소문자이면, 대문자로 바꿔서 arr 배열 안으로 집어넣는다. join()
: 배열의 모든 요소를 연결해 하나의 문자열로 만들어 출력하면 된다.
728x90