Print repeated string using javascript

function repeatStringNumTimes(str, num) {
   let newstr = '';
   if (num < 0) {
       return "";
     } else {
     for (let i = 0; i < num; i++ ) {
           newstr +=str;
       }    
     }
   return newstr;
 }
 console.log(repeatStringNumTimes("abc", 3)); // abcabcabc
 console.log(repeatStringNumTimes("*", 3)); // ***
 console.log(repeatStringNumTimes("*", -1)); 

Leave a Comment

Your email address will not be published. Required fields are marked *