Repeat Character N Times

后端 未结 23 2502
栀梦
栀梦 2020-11-22 11:51

In Perl I can repeat a character multiple times using the syntax:

$a = \"a\" x 10; // results in \"aaaaaaaaaa\"

Is there a simple way to ac

23条回答
  •  清酒与你
    2020-11-22 12:15

    this is how you can call a function and get the result by the helps of Array() and join()

    function repeatStringNumTimes(str, num) {
      // repeat after me
      return num > 0 ? Array(num+1).join(str) : "";
    }
    
    console.log(repeatStringNumTimes("a",10))

提交回复
热议问题