저장을 습관화

프로그래머스 LV.0 문자열 출력하기 본문

코딩 테스트/프로그래머스 - 자바스크립트

프로그래머스 LV.0 문자열 출력하기

ctrs 2023. 9. 7. 11:25

프로그래머스 LV.0 문자열 출력하기

https://school.programmers.co.kr/learn/courses/30/lessons/181952

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 명

문자열 출력하기


2. 문제 설명

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


3. 제한 사항

1 ≤ str의 길이 ≤ 1,000,000

str에는 공백이 없으며, 첫째 줄에 한 줄로만 주어집니다.


4. 예시

입력 #1

HelloWorld!

 

출력 #1

HelloWorld!

 

5. 기본 제공 코드

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


6. 제출한 내 답

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

 

6-2. VSC에 작성한 내용

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


7. 특이사항

이건 정말로 푸는 사람이 console.log 같은거 알고있나를 보기보다는

기본 제공 코드를 읽게끔 만드는 문제인것 같다

 

깊게 생각안하고 대충 이러면 되나? 하고 작성했는데

const solution = (str) => {
  console.log(str);
};

계속 틀리길래 당황했다

 

질문하기 부분에 가보니 다른 사람들도 혼란스러워 하고 있었고

질문 중 하나에 아래와 같은 답변이 달려 있었다.

이 글이 큰 힌트가 되었다.

읽고 다시 생각해보니 정말 입력은 어떻게 할 것이며 함수 호출은 어떻게 할 것인가에 대한 내용이 없었다.

 

프로그래머스 입력창을 초기화 하고 다시 읽어보니

stdin 표준 입력, stdout 표준 출력이 보였다.

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

솔직히 깊게는 모르는 내용이나

stdin이 할당되는 input과 stdout이 할당되는 output이 있으니

input이 입력되는 부분이고 output이 출력하는 부분이겠거니 했다.

 

그리고

let input = [];

rl.on('line', function (line) {
    input = [line];
}).on('close',function(){
    str = input[0];
    console.log(str)
});

배열 input을 초기화 하는 부분

함수 function을 불러와 line을 배열 input에 넣는 부분,

 

다시 함수 function을 불러오고 배열 input에 들어간 항목을 

변수 str에 들어가는 부분이 보였다.

 

그럼 변수 str이 입력받는 값이니까 얘를 출력만 해주면 되는걸까? 라고 생각했고

그게 정답이었다.

 

레벨 0의 가장 첫번째 문제이지만 정답률이 60%대인 이유가 느껴졌다

 

자바스크립트를 자세히 공부하지 않고 무작정 node.js만 공부한다면

막히는 부분이 많겠구나 하고 생각했다.

 

레벨 0를 넘기고 레벨 1, 레벨 2나 풀까 하는 생각도 다시 접게 되었다.

기본기를 탄탄히 하자.

 

그리고 기본 제공 코드를 소홀히 하지 말자

 


8. 다른 사람이 작성한 답

8-1.

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

 

8-2.

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

let input = [];

rl.on('line', function (line) {
    input.push(line);
}).on('close',function(){
    const str = input.join(' ');
    console.log(str);
});

 

위 사람들 처럼

코드를 읽고 바로 이해하고 활용할 수 있는 사람이 되자