JavaScript/프로그래머스
[프로그래머스/JS] Lv0. 문자열 반복해서 출력하기 (반복 출력)
동구름이
2024. 6. 21. 15:57
문제
https://school.programmers.co.kr/learn/courses/30/lessons/181950
풀이 & 소스 코드
문자열을 반복해서 출력하는 방식에는 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);
}