I did a very thorough search before asking this, but I couldn't find exactly what I was looking for. I want to preface that this is my fifth day coding in JavaScript, so be easy on me, please!
I'm taking a preparatory coding course for a full-fledged program and one of our objectives was to write a function repeatString(string, count)
that takes a string and the number of times you want it repeated as parameters. Here is my version with recursion:
function repeatString(string, count) {
if ( count === 0 ) {
return "";
}
if ( count === 1 ) {
return string;
}
if ( count > 1 ) {
return string + repeatString(string, count - 1);
}
}
We were then supposed to rewrite the function using while instead of recursion. This is where I got stuck:
function repeatString(string, count) {
var num = 0;
if ( count === 0 ) {
return "";
}
while ( num < count ) {
num += 1;
return string;
}
}
My current code returns a string only once no matter the count, unless the count is zero. I'm sure there is a glaring error right in front of me but my beginner eyes are not catching it.