如何在 javascript 循环中重复字符串

how to repeat string in javascript loop

我正在尝试编写一个函数,通过在该函数内创建一个循环来简单地重复字符串 3 次:

    repeatString('hey', 3) // returns 'heyheyhey'

到目前为止,我有以下代码并正在尝试进行循环;

    const repeatString = function() {

     }

   module.exports = repeatString

这是我想出并工作的代码!

const repeatString = function repeatString( str, num) {
str ='hey';
 if (num > 0) {
     return str.repeat(num); 
 } else if (num < 0) {
        return 'ERROR';
    } else {
        return '';
    }


 }


module.exports = repeatString

有点干净的方式Repeat function

const repeatString = function(str, num) {
     return (num < 0) ? new Error("Error") : str.repeat(num);
 }

module.exports = repeatString