Heggy Castaneda
Jul 27, 2017 · 1 min read

Repeat a string repeat a string

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) {
// set a holding pot string
var accumStr = '';
while(num > 0) {
accumStr += str; // accumStr = accumStr + "abc"
num--; // decrement by 1, num = num - 1
return accumStr;
repeatStringNumTimes("abc", 3);
Heggy Castaneda

Written by

like to build things

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade