Repeat a String - recursion not working :(

Tell us what’s happening:

hey guys, i am wondering why this one is not working, will appreciate any help :frowning:

Your code so far


function repeatStringNumTimes(str, num) {
  // repeat after me
  function recursive_add (str,num,output) {
   // console.log("hey",str,num,output)
  if (num<0) {
    return "xasd"
  }
  if (num>0) {
  //  console.log(output)
    num=num-1
    output=output+str
    recursive_add(str,num,output)
  }
  if (num==0)
  {
  //  console.log(output) -> this one returns correct output
    return output
  }

  }

  var x = recursive_add(str,num,"")
  console.log("should be correct but is empty :( ",x)
}

repeatStringNumTimes("abc", 3);


In the recursive case (if (num > 0)), you should return the output of recursive_add(...)

Your base case should return an empty string. It is, after all, repeating something zero times. Your base case should also be if (num <= 0) since some of the tests pass a negative number for the repeat count.

So for some python/pseudocode that translates pretty much directly to a JS solution: (please try to understand it before translating it straight to JS)

def repeat(str, count):
    if count <= 0: return ""
    return str + repeat(str, count - 1)