Repet string repet string

Repet string repet string
0

#1

function repeatStringNumTimes(str, num) {
// repeat after me
var tre="";
if(num<0) return “”;

for(var a = 1; a<=num; a++){
tre += str;
}

return tre;
}

r


#2

Hi paria,
if you could format your code by utilizing the three backticks prior to your code, and three backticks after, it would help us read your code a bit better.

A box like this will appear.

Also, what is your question, and what is your goal?


#3

Your version already passes all the tests.

You could refactor a little, though:

function repeatStringNumTimes(str, num) {
// repeat after me
  var tre = "";

  for (var a = 1; a<=num; a++) {
    tre += str;
  }
  
  return tre;
}

repeatStringNumTimes("abc", 3);
  • Remove the if...else because it’s not needed — the loop will run 0 times if the number is 0, because its conditions can’t be fulfilled
  • Swap out the while loop for a for loop, because for is more specialized and fits exactly what you need here

#4

ok.thanks lots:pray:


#5

what is your intention of my goal? goal of coding or …?