Skip to content

Repeat a String Repeat a String

Islam Ibakaev edited this page Apr 26, 2016 · 1 revision

Details

Repeat a given string (first argument) num times (second argument). Return an empty string if num is a negative number.

Elegant Solution

function repeatStringNumTimes(str, num) {
  return Array(num > 0 ? num + 1 : 0).join(str);
}
repeatStringNumTimes("abc", 3); // returns 'abcabcabc'
Clone this wiki locally