Implement String.prototype.repeat() with O(log n) concatenations by doubling

easyAbout 15 minutesUpdated CodingProstringspolyfillsbinary
Not solved

Share this question

Problem

"ab".repeat(3) returns "ababab". Write repeatString(str, count), which behaves like String.prototype.repeat, without using repeat, padStart, padEnd or Array(n).join.

repeatString("ab", 3);   // "ababab"
repeatString("ha", 0);   // ""
repeatString("x", 2.9);  // "xx"   (truncated to 2)
repeatString("x", -1);   // RangeError

Ref. UR-1432 · © UIRound · uiround.com/questions/implement-string-prototype-repeat-with-log-n-concatenations-by-doubling

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment