Repeat a String Repeat a String with concat() method not working

Tell us what’s happening:

The string concat() method seem to work but not inside of the for-loop in the repeatStringNumTimes() function.

Is there a reason for that?

Your code so far


function repeatStringNumTimes(str, num) {
  // repeat after me
  var retStr = "";
  for(var i = 0; i < num; i++){
    retStr.concat(str);
    //retStr += str;
  }
  console.log(retStr);
  //Output empty
  return retStr;
}

repeatStringNumTimes("abc", 3);

let hello = 'Hallo, '
console.log(hello.concat("Dude"));
//Output: Hallo, Dude

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/repeat-a-string-repeat-a-string

concat() doesn’t mutate. It returns a new value.

2 Likes