Tell us what’s happening:
So I’m working on the repeat a string challenge. I’m a noob, so apologies in advance if I have a tough time conveying my intentions.
I split the string into an array, with the hope of joining it back into a string once I had concatenated the array to itself the correct number of times.
I first tried using a for loop (the commented out section), but that only concatenated it once as long as num was less than i, which makes sense to me after I thought through it.
Then I took a similar approach with a while loop, but that returns nothing when I run it.
I’ve also tried using .push in place .concat, but that just returns a number, which also is a mystery to me, but I’m trying to do one thing at a time at the moment.
If anyone could point me in the right direction, I’d greatly appreciate it. Thanks!
Your code so far
function repeatStringNumTimes(str, num) {
var splitStr = str.split();
var allSplit = splitStr;
var joinMe = [];
var i=0;
while (i < num){
allSplit.concat(splitStr);
i++;
}
// for (var i=0; i < num ; i++)
// if (i <= num);
// {joinMe = allSplit.concat(splitStr);}
if (num <= 0) {
return "";
}
else return (
joinMe.join("")
);
}
repeatStringNumTimes("abc", 5);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0
.
Link to the challenge:
https://www.freecodecamp.org/challenges/repeat-a-string-repeat-a-string