[해결로 표시] 문자열을 JavaScript로 N번 반복하기

  

3
주제 스타터

Write a JavaScript function that repeats a given string, N times.

The input comes as 2 arguments:

  • The first argument is a string that will represent the one you need to repeat.
  • The second one is a number will represent the times you need to repeat it.

예시:

입력:
repeat
5

출력:
repeatrepeatrepeatrepeatrepeat


입력:
magic is real
3

출력:
magic is realmagic is realmagic is real


The output is a big string, containing the given one, repeated N times.

1개 답글
2

My JS code:

function repeatStr(input, times) {
    let result = input.repeat(times);
    console.log(result);
}
 
repeatStr("repeat", 5);

and another solution: 

function repS(data, howmany) {
    let endResult = "";
 
    for (let i = 1; i <= howmany; i++) {
        endResult += data;
    }
    console.log(endResult);
 
}
 
repS("magic is real", 3);
공유: