Why would this return in multiple lines?

Tell us what’s happening:

I checked the solution and seen the correct way of writing this recursively, but I’m curious why this returns in multiple lines. Is there a way to put everything into one?

Your code so far

function repeatStringNumTimes(str, num) {
if (num <= 0) return []
else if (num === 1) console.log(str)
else console.log(str) + repeatStringNumTimes(str, num - 1)

repeatStringNumTimes("abc", 3);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Repeat a String Repeat a String

Link to the challenge:

It repeats multiple lines because every time console.log is called, it creates a new line. Console.log is called 3 times.
If you want to create one line, try to concatenate your strings to one var, and only log once at the end.
So where you’d write the console.log now, it would be something like var myString = myString + “abc”

1 Like

Thanks, for the clarification, cool insight!