Repeat Character N Times

后端 未结 23 2782
栀梦
栀梦 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条回答
  •  -上瘾入骨i
    2020-11-22 12:33

    Right pads with zeros with no arrays or loops. Just uses repeat() using ES6 2015, which has wide support now. Left pads if you switch the concatenation.

    function pad(text, maxLength){ 
      var res = text + "0".repeat(maxLength - text.length);
      return res;
    }
    
    console.log(pad('hello', 8)); //hello000
    

提交回复
热议问题