연습장

01. 문자열 출력하기 본문

프로그래머스/0단계

01. 문자열 출력하기

js0616 2023. 6. 16. 23:15

사용언어 : JavaScript 

 

Q: 문자열 str이 주어질 때, str을 출력하는 코드를 작성해 보세요.

 

입력

HelloWorld!

출력

HelloWorld!

 

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];
});


 

기본으로 제공되는 긴 코드가 

console 창에서 입력을 받고 출력을 하는 코드라는데 솔직히 이해가 잘 안된다.

검색해보니 Node.js 와 관련이있는거같은데 아직 덜배워서 잘 모르겠다. 

 


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];

    console.log(str) // console.log 를 통해 출력 가능하다. 
});

 


 

파이썬의 경우 

str = input()
print(str)

 

로 끝나는 문제가 

Js 에서 저렇게 긴 코드가 되는걸 보면서 이게 뭐지?  싶은 생각이 든다. 

코드가 짧을수록 좋다고 하던데 ....

 


다른사람 코드

 

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

 

음.. function을 정의하는 대신 console.log 를 넣은것 같은데 잘 이해가 안간다. 

 

'프로그래머스 > 0단계' 카테고리의 다른 글

05. 특수문자 출력하기  (0) 2023.06.17
04. 대소문자 바꿔서 출력하기  (0) 2023.06.17
03. 문자열 반복해서 출력하기  (0) 2023.06.16
02. a와 b 출력하기  (0) 2023.06.16
프로그래머스 0단계  (0) 2023.06.16