Tell us what’s happening:
I want the for loop to run three times(or as many times is specified in the second parameter of the function). For some reason it is not working.
I want to concatenate the string as many times it runs in the for loop.
Your code so far
function repeatStringNumTimes(str, num) {
var str1 = str ;
if(num < 0){
return "" ;
}else{
for(var i = 1 ; i<= num ; i++)
{
var str2 =str.concat(str1);
}
console.log(str2);
}
}
repeatStringNumTimes("abc", 3);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.
you need do define str2 outside the loop if you want to do this. Also, you are just always setting it to be equal to str.concat(str1), instead to change str2 based on previous value you need to have str2 = str2 ...