Repeat string challenge

Repeat string challenge


Can someone explain why this works only for the positive numbers? I fail the test when it gets to num=-2, but I thought adding the “if” function would take care of that. Thanks!

function repeatStringNumTimes(str, num) {
var anythingString = String(str);
var answer = anythingString.repeat(num);
if (num >0) {return answer;} else return “”;

repeatStringNumTimes(“abc”, 3);


@cocacolna What you have here will return an empty string "" if num is zero or less. Is that what you want?

Also, I think you can solve the problem by using an absolute value method: Math.abs(num).


You need to check for the negative number at the start of the function and return the blank string then. The rest of your code is fine. Of course you will just need to return answer at the end without the if/else statement.

FYI - There is no need to use String(str), because the challenge says the first argument will be a string.