Reapeat a string _ Not returnniung empty string

Hello everyone.
I am facing some difficulties with the algorithm repeat a string. Bellow is my code and I don’t know why it doesn’t return an empty string when num is smaller than 0.

function repeatStringNumTimes(str, num) {

var foo = str.repeat(num);
var empty = “”;

if (num < 0 ){
return empty;
} else {
return foo;
}

}

repeatStringNumTimes(“abc”, 3);

I try without creating a var empty but again it didn’t work.

Can someone tell me what’s wrong?

The negative number in str.repeat(n) throws a range error. Just reorder your control logic so str.repeat is only called when n is positive. If n < 0 return blank; else return str.repeat(n).

Thank you very much!!!