Concat() function

Tell us what’s happening:
Describe your issue in detail here.
I have tried using the concat() function and checked the output with console.log(), however, the result turned out to be an array of abc’s instead of abcabcabc. I have just started working on this course after a long break so I googled what I could use to add to the end of the string and the first example on this website gave me the assumption that concat would succeed at what I am trying to do since it is TechOnTheNet, not Tech, On, The, Net. Here is the link to the website:

  **Your code so far**

function repeatStringNumTimes(str, num) {
let myStr = [];
if(num <= 0) return myStr;
else for(let i = 0; i < num; ++i) {
myStr = myStr.concat(str);
return myStr;

repeatStringNumTimes("abc", 3);
  **Your browser information:**

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

Challenge: Repeat a String Repeat a String

Link to the challenge:

you create an array let myStr = [] and then use the array method concat on it- myStr.concat(str), which in the end will net you an array . Just because you call it ‘str’ doesnt make it a string. Id say using the string method concat is unnecessary, you can simply say str1+str2, which will concat the two strings.

How do I declare a string then? Also, I am confused why we can’t just edit the given string and have to create a new one.

if you have fundamental gaps, i advise you start from the begining of the JS curriculum. let str=''. None say you cant edit the current string, but its more convenient to store its initial value in a variable, to either use as a starting point, add N times str to it, or add it to str N times to reach the required result. Without storing it in another variable, i can only think of using recursion to reach the result, but that would be slightly more complicated to thinker.

1 Like

because strings are immutable

1 Like