Repeat a string repeat a string - Hint pls

Repeat a string repeat a string - Hint pls
0

#1

Hi there. Without telling me the answer I’m wondering if someone can give me a hint on where to look at this function to fix it. I’m not dealing with the iff statement yet as I feel that will be simple. I’m just trying to get the string correct first. I feel like I’m close.

In case it’s not clear this is for the Repeat a string repeat a string algorithm.

Thanks!

Your code so far

function repeatStringNumTimes(str, num) {
  var myString = str;
  var myNum = num;
  for(var i = 0; i < myNum; i++){
    
    myString += myString;
      return myString;
  }

}

repeatStringNumTimes("*", 8);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:


#2

I also looked at declaring myString like this. Not sure if it’s better but thinking so. Is the for look conditions setup incorrectly?


#3

I feel a little silly. I found the error. Placing returns where they need to go makes a diff I guess. :slight_smile:

function repeatStringNumTimes(str, num) {
var myString = ‘’;
var myNum = num;
for(var i = 0; i < myNum; i++){

myString += str;

}
return myString;
}

repeatStringNumTimes("*", 8);


#5

Hi, there are a few problems in your code
1, Your return is in your loop, which you have changed
2, You assigned the str to myString, not the "
3,You writed the myString+=myString in the first case, which means myString=myStirng+myString;
but in the loop, myStirng is a var, it doubles itself. So you have to plus the str instead of myString, which you have changed in the secound case.
4,Conside your i begaining at 0. myString += str, it has a str in itself, so you should begain it at 1
5,You forgot the negative condition