JavaScript/프로그래머스

[프로그래머스/JS] Lv0. 문자열 반복해서 출력하기 (반복 출력)

동구름이 2024. 6. 21. 15:57

문제

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

 

프로그래머스

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

programmers.co.kr


풀이 & 소스 코드

문자열을 반복해서 출력하는 방식에는 3가지 정도가 있다.

 

1. 문자열 누적 방식

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]);
    let temp = '';
    for(let i = 0; i<n;i++){
        temp+=str;
    }
    console.log(temp);
});

 

 

2. repeat() 메서드 사용

    str = input[0];
    n = Number(input[1]);
    console.log(str.repeat(n));

 

 

3. process.stdout.write

    str = input[0];
    n = Number(input[1]);
    for (i=0; i<n; i++){
        process.stdout.write(str);   
    }