Hello Friends Whats is Wrong with my Code?

*Repeat a String n Times
I created this basic code which is working on VS, but running the FreeCodeCamp Test doesn’t, please can someone give me a feedback.

  **Your code so far**

function repeatStringNumTimes(str, num) {  
let newStr = ' ';
if(num > 0){
  for(let x = 0; x < num; x++){
     newStr = newStr + str;
  }
}
return newStr;
}

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

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Repeat a String Repeat a String

Link to the challenge:

Printing out the result in this manner should give some idea what’s wrong:

console.log('"' + repeatStringNumTimes("abc", 3) + '"');
1 Like

Remove the spacing inside newStr.
It should be empty as in
let newStr="";

Your code works for all numbers but fails for negative numbers since negative number should return an empty string without spacing

1 Like

Very appreciated my friend, now is working as requested.!

God bless you and happy coding. !

1 Like