Struggling with code for "Repeat a string" challenge

Struggling with code for "Repeat a string" challenge
0

#1

Hi guys, I’m really stumped with this one. I’m resisting the temptation to hit up the blog posts and wikis on this challenge because it seems really simple and I should have no problem with it. That said, my code is just returning an empty string and I have no idea why.

Here it is:

function repeatStringNumTimes(str, num) {
  var returnStr = "";
  if (num < 1) {
    return "";
  } else {
    for (i = 0; i < num; i++) {
    returnStr.concat(str);
  }
  return returnStr;
  }
}

repeatStringNumTimes("abc", 3);

I know that this isn’t caused by the if statement’s empty string because this persists even if I comment-out the if statement. What am I doing wrong here?


#2

you never actually assign anything to returnStr after you have declared it.

returnStr.concat(str); will be ABC in that instance per say.
but when the loop continues returnstr is still empty.
You need to save your concated string somewhere while you loop through it.


#3

Gah I knew it would be something obvious, thanks for the quick response!