# freeCodeCamp Algorithm Challenge Guide: Repeat a string repeat a string

freeCodeCamp Algorithm Challenge Guide: Repeat a string repeat a string
0

#11

I noticed that a few people posted solutions that are VERY SIMILAR to the FCC solution, but using `for` loops with decalred `var i` rather than `while` loops incrementing `num` itself. @TatheerHussain @irakligeek

I ran some performance tests, and I found that using a `for` loop instead of a `while` loop takes almost TWICE as long! question is: why? is it because you are declaring a second variable in the `for` loop? is it that much faster just to use `num` instead of introducing `i` as well? or is there something about the difference between the two loops that I don’t understand?

1,000,000 repetitions of each function
with `for` loop and `var i`: 64.7 milliseconds
with `while` loop and incrementing `num`: 34.2 milliseconds

10,000,000 repetitions of each function
with `for` loop and `var i`: 562.6 milliseconds
with `while` loop and incrementing `num`: 313.6 milliseconds

100,000,000 repetitions of each function
with `for` loop and `var i`: 5.5 seconds
with `while` loop and incrementing `num`: 3.09 seconds

1,000,000,000 repetitions of each function
with `for` loop and `var i`: 54.8 seconds
with `while` loop and incrementing `num`: 30.7 seconds

#12

I solved this challenge with this:

function repeatStringNumTimes(str, num) {
// repeat after me
var more = ‘’;
for(var i = 0; i < num; i++){
if(num > 0){
more += str;
}else{
return ‘’;
}
}
return more;
}

repeatStringNumTimes(“abc”, 1);

#13

Basicly your top solution doesn’t work because the repeat function only allows values >= 0.
If num is negative it evaluates to an error : Uncaught RangeError: Invalid count value , and therefor JavaScript
wont progress.

So basicly the 2nd solution is the correct way, but you could have solved it that way too:

// store the function in re_peat with (str,num) parameters and return the result of str.repeat(num)
var re_peat = function (str, num) {
return str.repeat(num);
};

// still check if num > 0 to call the function with the Parameters, because see reason above.
if (num > 0)
{
return re_peat(str,num);
}
else
{
return “”;
}

Hope that helped.

#14

USING A FOR LOOP:
function repeatStringNumTimes(str, num) {
// repeat after me
var a="";
if (num>0)
{
for(i=1;i<=num; i++){
a += str;
}

}
return a;
}

repeatStringNumTimes("\$", 32);

#15

function repeatStringNumTimes(str, num) {
var texto ="";
if(num<0){
str = “”;
} else {
for(var i=0;i<num;i++){
texto+=str;
}
}
return texto;
}

repeatStringNumTimes(“abc”, 3);

#16
``````function repeatStringNumTimes(str, num) {

// repeat after me
if (num <= 0) return "";

return new Array(num+1).join(str);
}``````

#17

I solved it this way, but thanks to @jmbothe, I’ll switch to `while` loops going forward.

`function repeatStringNumTimes(str, num) { var newString = ''; // repeat after me for (i = 0;i < num; i++) { newString = newString + str; } return newString; } repeatStringNumTimes("abc", 3);`

#18

My solution ended up being an unoptimized version of advanced code solution using `repeat` method

``````function repeatStringNumTimes(str, num) {

//Positive num only
if (num <= 0){
return "";
}

return str.repeat(num);
}

repeatStringNumTimes("abc", 3);
``````

#19

This is how I solved it. The str.repeat pretty much solved the issues for me.

#20

The code I wrote seems to get the correct answer but the page wont recognise me passing. Any idea?

acc = “”;

function repeatStringNumTimes(str, num) {
if (num > 0) {
for (i = 0; i != num; i++) {
acc += str;
}
return acc;
}
return “”;
}

repeatStringNumTimes(“abc”, 3);

#21

yes it worked… it should work with yours.

#22

Here is my solution at the end i found repeat() function and i used it
// repeat after me
/var st="";
if(num < 0)
return “”;
for(var i = 0; i < num; i++){
st+=str;
}
return st;
/
// OR
if(num < 0)
return “”;
else
return str.repeat(num);
Here is another way with loop

``````function repeatStringNumTimes(str, num) {
// repeat after me
var s ="";
if(num < 0){return s;}
for(var i = 0; i < num; i++){
s+= str;
}
return s;
}
``````

#23

Did that too…

It is that a “wrong” way to do it?

#24

Came up with two answers, one short and one long:

1. Short
function repeatStringNumTimes(str, num) {
return (num < 0) ? ‘’ : str.repeat(num);
}

2. Long
function repeatStringNumTimes(str, num) {
if (num < 0) {
return ‘’;
} else {
var result = [];
for (var i = 0; i < num; i++) {
result = result.concat([str]);
}
return result.join(’’);
}
}

#25

Too much code or not ?

function repeatStringNumTimes(str, num) {
// repeat after me

var newStr = str.split(’ ');
var result;

if (num > 0) {
for (var i = 1; i < num; i++) {
newStr.push(str);
}

``````result = newStr.join('');
``````

} else {
result = ‘’;
}
return result;
}

repeatStringNumTimes(“abc”, 1);

#26

This worked for me simple for loop.

``````function repeatStringNumTimes(str, num)  {
// repeat after me

if(num >= 0){

var rep = "";

for(var i = 0; i < num; i++){

rep = rep + str;

}

return rep;

}

else

return "";

}``````

#27

My take:

function repeatStringNumTimes(str, num) {
// repeat after me

var repeatStrNum = “”;

while(num > 0) {
repeatStrNum += str;

``````num--;
``````

}
return repeatStrNum;
}

repeatStringNumTimes(“abc”, 3);

#28

Thus is my code,Ive seen better ways thanks campers.
function repeatStringNumTimes(str, num) {
// repeat after me
nr = “”;
if(num < 0){
return nr;

}else{
return str.repeat(num);
}

}

repeatStringNumTimes(“abc”, 3);

#29
``````function repeatStringNumTimes(str, num) {
return num > 0 ? str.repeat(num) : "";
}``````

#30

Not sure if you got things figured out, but your variable “acc” should be declared within the function (yours is outside the function, just above it). You probably don’t need that second “return ‘’;” either.