Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joePichardo/50441aa95305c15091fc to your computer and use it in GitHub Desktop.
Save joePichardo/50441aa95305c15091fc to your computer and use it in GitHub Desktop.
Repeat a given string (first argument) n times (second argument). Return an empty string if n is a negative number.
// Bonfire: Repeat a string repeat a string
// Author: @joepichardo
// Challenge: http://www.freecodecamp.com/challenges/bonfire-repeat-a-string-repeat-a-string
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function repeat(str, num) {
// repeat after me
//creates an empty string to return
//if num is less than 0
//if num isn't string is add "num" times
//then returned
var addToString = "";
if(num > 0){
for(var i = 0; i < num; i++){
addToString += str;
}
return addToString;
}
return addToString;
}
repeat("abc", 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment