Daily Logs/Algorithm Practice

[level 0] 문자열 곱하기

Jcob.moon 2025. 5. 18. 12:15

function solution(my_string, k) {
    const result = my_string.repeat(k);

    return result;

}
    //my_string 을 k 값만 큼 반복하려면 repeat(k) 를 이용하여 반복
  • 주요문법 :String.prototype.repeat()
"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)

링크 : MDN
깃허브
프로그래머스