문제 1
문자열 str 이 주어질 때, str 을 출력하는 코드를 작성해 보세요.
▶ 내가 푼 방식
//내가 작성한 코드
const readline = require('readline');
cosnt rl = readline.createInterface({
input: process.stdin,
output: process.stdout
}
let input = [];
rl.on( 'line', function(line){
input = [line];
}).on( 'close', function(){
str = input[0];
console.log(str)
});
▶ 다른 유저가 푼 방식
// 유저 1
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
}).on('line', console.log)
문제 2
주어진 문자열 과 정수를 이용하여 정수만큼 문자열을 반복하여 출력하세요
▶ 내가 푼 방식
//내가 작성한 코드
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
str = input[0];
n = Number(input[1]);
result = [];
for(let i =0; i<n; i++){
result.push(str)
}
console.log(result.join(''))
});
▶ 다른 유저가 푼 방식
// 유저 1 : repeat
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
str = input[0];
n = Number(input[1]);
console.log(str.repeat(n));
});
// 유저 2 : process.stdout.write
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input = [];
rl.on('line', function (line) {
input = line.split(' ');
}).on('close', function () {
str = input[0];
n = Number(input[1]);
for (i=0; i<n; i++){
process.stdout.write(str);
}
});
// 유저 3
▶ 배운 것들
- repeat 함수로 반복 가능
- process.stdout.write() 도 가능
문제 3 대문자소문자 바꿔서 출력하기
영어 알파벳으로 이루어진 문자열 str 이 주어집니다.
각 알파벳을 대문자는 소문자로 소문자는 대문자로 변환해서 출력하는 코드를 작성해 보세요.
▶ 내가 푼 방식
//내가 작성한 코드
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];
for(let i =0; i < str.length; i ++){
const word = /[A-Z]/.test(str[i]) ? str[i].toLowerCase() : str[i].toUpperCase();
process.stdout.write(word)
}
});
▶ 다른 유저가 푼 방식
// 유저 1
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input;
rl.on('line', (line) => {
input = [...line];
}).on('close', () => {
console.log(
input.map((char) => (/[a-z]/.test(char) ? char.toUpperCase() : char.toLowerCase())).join(''),
);
});
//유저 2
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];
const regex = /[A-Z]/
console.log([...str].map((v)=> regex.test(v) ? v.toLowerCase() : v.toUpperCase()).join(''))
});
▶ 배운 것
- 스프레드 방식으로 문자를 배열로 만들수 있다는 점
문제 4
!@#$%^&*(\'"<>?:; 출력하기
▶ 내가 푼 방식
//내가 작성한 코드
// !@#$%^&*(\'"<>?:; 을 출력하기 위해서 \ 추가
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let input;
rl.on('close', function () {
const output = `!@#$%^&*(\\'"<>?:;`
process.stdout.write(output)
});
'프로그래밍 > 코딩연습' 카테고리의 다른 글
[ JS 코딩연습 ] 2024/04/07 : 3. 연산 및 출력 (1) | 2024.04.07 |
---|---|
[ JS 코딩연습] 2024/04/07: 2. 제곱수 판별하기 (1) | 2024.04.07 |
[ JS 코딩연습 ] 2024/04/06: 3. 편지 (0) | 2024.04.06 |
[ JS 코딩연습 ] 2024/04/06 : 2. 중복된 숫자 개수 (0) | 2024.04.06 |
[ JS 코딩연습 ] 2024/04/06 : 1. 배열 원소의 길이 (0) | 2024.04.06 |