Skip to content

Instantly share code, notes, and snippets.

@bflannery
Created December 21, 2016 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bflannery/255e9b255b5b505e30734d29f2128e56 to your computer and use it in GitHub Desktop.
Save bflannery/255e9b255b5b505e30734d29f2128e56 to your computer and use it in GitHub Desktop.
Repeat a String X Number of Times
// Repeat a given string (first argument) num times (second argument). Return an empty string if num is not a positive number.
function repeatStringNumTimes(str, num) {
let finalStr = "";
while(0 < num) {
finalStr += str;
num--;
}
return finalStr;
}
console.log(repeatStringNumTimes("abc", 3));
console.log(repeatStringNumTimes("*", 8));
console.log(repeatStringNumTimes("abc", -2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment