Tell us what’s happening:
Is there a better solution for this?
Better recursion would be nice, as recursion seems very tough to me and so I am trying it.
Your code so far
function repeatStringNumTimes(str, num) {
if (num <= 0) {
return "";
} else {
str = repeatStringNumTimes(str, num - 1) + str
}
return str
}
console.log(repeatStringNumTimes("abc", 4));
You do not really need the above or the closing bracket }. Instead of reassigning the result of the recursive call to str + str, just return all of it.
Before posting a working solution for comparison purpose, you should try to compare your solution to a solution on the Get a Hint page.
The third solution is basically the same as yours, but uses a ternary expression.