freeCodeCamp Challenge Guide: Repeat a String Repeat a String

yes it worked… it should work with yours.

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;
}

Did that too…

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

1 Like

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(’’);
    }
    }

1 Like

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);

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 "";

}

My take:

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

var repeatStrNum = “”;

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

num--;

}
return repeatStrNum;
}

repeatStringNumTimes(“abc”, 3);

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);

function repeatStringNumTimes(str, num) {
  return num > 0 ? str.repeat(num) : "";
}
1 Like

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.

I used this to complete the challenge. Very simplistic and basic.

function repeatStringNumTimes(str, num) {
// repeat after me
var x = ‘’;
while (x > num){
return ‘’;
}
if (x < num)
return str.repeat(num);
}

repeatStringNumTimes(“abc”, -3);

function repeatStringNumTimes(str, num) {
  var repeat = "";
  for(var i=0;i<num;i++){
    if(num<1){
	return "";
	}
	else  repeat = repeat.concat(str);
  }
  return repeat;
}

repeatStringNumTimes("abc", 0);
1 Like

I came up with the same code.

1 Like

I did the same as well. Tbh I like the look of this code more than the intermediate, but I’m unsure if the intermediate code is “better”.

I like advanced best just because of lines of code used but, still…

function repeatStringNumTimes(str, num) {
  // repeat after me
  let strHolder ="";
  for(let i = 0; i < num; i++){
    strHolder += str; 
  }
  return strHolder;
}