Created
December 21, 2016 00:03
-
-
Save bflannery/255e9b255b5b505e30734d29f2128e56 to your computer and use it in GitHub Desktop.
Repeat a String X Number of Times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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