Why won't my code work? - Repeat a String Repeat a String -

Ok, so on the Repeat a String, my code as follows won’t work:

function repeatStringNumTimes(str, num) {
  let newStr = " ";
  for (var i=0; i < num; i++) {
      newStr += str;
  return newStr;

When I use the javascript test window, it works fine, but when I run the tests through the fcc window, it fails on many.

Any ideas why?

it is because your starting value is a space.
so if you have repeatStringNumTimes("moo", 3), the string returned is actually " moomoomoo" instead of "moomoomoo"
and " moomoomoo" === "moomoomoo" is false, so the tests fail

1 Like

Oh my! Thank you!
I seem to do that a lot. I always set an empty string to a space, and that’s not empty. :woman_facepalming:

You can use JS function repeat.