Repeat a String - Node.js String

Node.js examples for String:Repeat

Description

Repeat a String

Demo Code


String.prototype.repeat = function(count) {
    if (count < 1) return '';
    var result = '', pattern = this.valueOf();
    while (count > 1) {
        if (count & 1) result += pattern;
        count >>= 1, pattern += pattern;
    }//from  w w w .  j av  a2 s. co  m
    return result + pattern;
};

Related Tutorials