[프로그래머스] 문자 반복 출력하기

vancouver·2023년 3월 30일
0

문자 반복 출력하기

문제설명

-문자열 my_string과 정수 n이 매개변수로 주어질 때, my_string에 들어있는 각 문자를 n만큼 반복한 문자열을 return 하도록 solution 함수를 완성해보세요.


제한사항

  • 2 ≤ my_string 길이 ≤ 5
  • 2 ≤ n ≤ 10
  • "my_string"은 영어 대소문자로 이루어져 있습니다.

입출력의 예

my_stringnreturn
"hello"3"hhheeellllllooo"

입출력 예 설명

입출력 예 #1

  • "hello"의 각 문자를 세 번씩 반복한 "hhheeellllllooo"를 return 합니다.

풀이

const solution = (my_string, n) => [...my_string].map(a => a.repeat(n)).join("")

String.prototype.repeat()

  • repeat() 메서드는 문자열을 주어진 횟수만큼 반복해 붙인 새로운 문자열을 반환합니다.

구문

str.repeat(count);

매개변수

count

  • 문자열을 반복할 횟수. 0과 양의 무한대 사이의 정수([0, +∞)).

예제

'abc'.repeat(-1);   // RangeError
'abc'.repeat(0);    // ''
'abc'.repeat(1);    // 'abc'
'abc'.repeat(2);    // 'abcabc'
'abc'.repeat(3.5);  // 'abcabcabc' (count will be converted to integer)
'abc'.repeat(1/0);  // RangeError

({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2);
// 'abcabc' (repeat() is a generic method)

Reference

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

0개의 댓글