So I decided to use recursion for this challenge. The strings repeat the correct amount of times and it loops fine. My only real issue that I can see is that instead of printing everything on a single line as required, it’s being printed in separate lines.
function repeatStringNumTimes(str, num) {
if (num <= 0) {
return "";
} else {
repeatStringNumTimes(str, num - 1);
console.log(str)
return str;
}
}
repeatStringNumTimes("abc", 3);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Repeat a String Repeat a String
Please note by the tests that what the problem is asking is to take a string and concatenate it num times, and not to return it three times.
If you have a string ‘test’ and num = 3, you have to figure out a way to return testtesttest
I thought I had to return str but I actually had to just return str plus the function. So I think I was sort of on the right track originally but got confused somewhere.
function repeatStringNumTimes(str, num) {
if (num <= 0) {
return "";
} else {
return str + repeatStringNumTimes(str, num - 1);
}
}
repeatStringNumTimes("abc", 3);