상세 컨텐츠

본문 제목

<자바스크립트> 코딩 기초 트레이닝 Day1 (2~5)

Programming language/자바스크립트 문제 풀이

by 주초위왕 2024. 1. 26. 23:54

본문

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];

rl.on('line', function (line) { // 문자열 => "4 5"
    input = line.split(' '); // input[0] = "4" , input[1] = "5"
}).on('close', function () {
    console.log("a = " + Number(input[0]));
    console.log("b = " + Number(input[1]));
});

 

나의 풀이

1. rl.on('line', function (line)

문자열 4와 5를 입력하면

 

2.  input = line.split(' ');

받은 문자열이 line에 입력이 되고 split메소드로 하나씩 쪼개어 input의 배열 0번째 4가 저장이 되고, 1번째에 5가 저장이 된다.

 

3. input에는 ["4", "5"]가 저장 됨

 

4. console.log("a = " + Number(input[0]));
    console.log("b = " + Number(input[1]));

그럼 차례데로 문자열 "a = "이 나오게하고 input에 저장된 배열을 하나씩 꺼내 출력한다.

 


let input = [];
rl.on('line', function (line) { // line = "string 5"
    input = line.split(' ');  // 4 5 -> input[0] = string, input[1] = 5    
}).on('close', function () {
    str = input[0];
    n = Number(input[1]);
    
    console.log(str.repeat(n));
});
나의 풀이

1. input = line.split(' ');

"string 5"을 입력하면, ["string", "5"]배열로 input에 초기화 된다.

 

2. str = input[0];
    n = Number(input[1]);

그럼 변수 str에 input[0]번째에 출력될 string이 들어가고, 변수 n에 input[1]번째에 5가 들어간다.

 

3. string이 저장된 srt을 연속으로 출력할 메소드 repeat을 입력하고 몇번 출력할지 (n) => 5 을 넣으면

n만큼 string이 출력된다.


let input = [];

rl.on('line', function (line) {
    input = line.split('');
    input.forEach((e, index)=>{
        if(e !== e.toLowerCase()) { 
            input[index] = e.toLowerCase();
        } else {
            input[index] = e.toUpperCase();
        }   
    })
    console.log(input.join(""));
나의 풀이

1. input = line.split('');

aBcDeFg를 입력하면 하나로 쪼개져 [ "a", "B", "c", "D", "e", "F", "g"] 배열이 생성되고 input에 저장된다.

 

2. input.forEach((e, index)=>{

input안에 배열로 쪼개진 문자열들을 e변수 안에 원소로 하나씩 보내준다, index또한 0부터 하나씩 보내주기

 

3. if(e !== e.toLowerCase()) { 
            input[index] = e.toLowerCase();
        } else {
            input[index] = e.toUpperCase();
        }

input배열의 원소 "a"가 e변수 안에 넣어서 보내지고 0번째부터 시작하는 index가 "a"의 0번째도 같이 보내진다.

만약에 "a"가 a의 소문자와 일치하지 않는다면 "a"가 소문자로 변환해서 input의 [index]의 0번째에 들어가고

그렇지 않는다면 "a"가 대문자로 변환되어 input의 [index]의 0번째에 들어간다.

반응형

댓글 영역