Repeat Character N Times

后端 未结 23 2547
栀梦
栀梦 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:34

    An alternative is:

    for(var word = ''; word.length < 10; word += 'a'){}
    

    If you need to repeat multiple chars, multiply your conditional:

    for(var word = ''; word.length < 10 * 3; word += 'foo'){}
    

    NOTE: You do not have to overshoot by 1 as with word = Array(11).join('a')

提交回复
热议问题