Basic Algorithm Scripting:Repeat a String Repeat a String

Basic Algorithm Scripting:Repeat a String Repeat a String
0

#1

Tell us what’s happening:
What is wrong with this code ? I see in the console, it is returning the required output.

Your code so far


function repeatStringNumTimes(str, num) {
  // repeat after me
  if (num < 0) {
    console.log("");
  }
  else {
  var strArr = [];
  for (var i=1; i<=num; i++) {
    strArr.push(str);
  }
  return console.log(strArr.join(''));
  }
}

repeatStringNumTimes("abc", 3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 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


#2

Your main problem is you are console logging the result instead of returning it.
For exampe this line returns undefined: return console.log(strArr.join(''));

Change all console logs to returns and the code will pass the tests.
Also just as a suggestion, pushing things into an array may not be necessary here because you can simply concatenate strings.

for example like this

let result = "";
for (let i = 0; i < num; i++) {
  result += str;
}
return result;