Nodejs String Repeat repeat(count)

Here you can find the source of repeat(count)

Method Source Code

// You're working on some interesting console application and you want to line up some of your lines just right.
// A lot of your data is nested down so you've got code like this all over:

// console.log('Root ->');
// console.log('    Sub-node');
// console.log('    Sub-node->');
// console.log('        Sub-sub-node');
// .../*from  w  w  w  .j  a  v  a2s.c om*/

// but that just looks terrible and doesn't scale well for variable levels of nesting.
// Oh, if only you could write something more like:

// console.log('Root->');
// console.log(' '.repeat(4) + 'Sub-node');
// console.log(' '.repeat(4) + 'Sub-node->');
// console.log(' '.repeat(8) + 'Sub-sub-node');
// ...
// Oh, that's right. You can.

// for loops are cool, I guess. Other kinds of solutions are a lot cooler.
String.prototype.repeat = function(count) {
  if (count === 1) return this;
  return this + this.repeat(count - 1);
};

Related

  1. repeat(count)
    String.prototype.repeat = function(count) {
      return new Array(count + 1).join(this); 
    };
    
  2. repeat(count)
    var range = function(k) {
        return Array.apply(null, Array(k)).map(function(_, i) {
            return i;
        });
    };
    String.prototype.repeat = function(count) {
        if (count < 1) return '';
        var result = '', pattern = this.valueOf();
        while (count > 1) {
    ...
    
  3. repeat(count)
    String.prototype.repeat = function(count) {
      if (count < 1) return '';
      var result = '', pattern = this.valueOf();
      while (count > 0) {
        if (count & 1) result += pattern;
        count >>= 1, pattern += pattern;
      return result;
    };
    ...
    
  4. repeat(count)
    String.prototype.repeat = function(count) {
      if (count < 1) return '';
      var result = '', pattern = this.valueOf();
      while (count > 0) {
        if (count & 1) result += pattern;
        count >>= 1, pattern += pattern;
      return result;
    };
    ...
    
  5. repeat(count)
    String.prototype.repeat = function(count) {
        if (count < 1) return '';
        var result = '', pattern = this.valueOf();
        while (count > 0) {
            if (count & 1) result += pattern;
            count >>= 1, pattern += pattern;
        return result;
    };
    ...
    
  6. repeat(count)
    String.prototype.repeat = function(count) {
     return (count === 1) ? this : this + this.repeat(count-1);
    };
    console.log('x'.repeat(8) + 'Sub-node');
    
  7. repeat(count)
    String.prototype.repeat = function(count) {
      var result = '';
      for (var i = 0; i < count; i++) {
        result += this;
      };
      return result;
    console.log("*".repeat(5).padLeft(10, "-").padRight(15, "+"));
    
  8. repeat(count)
    String.prototype.repeat = function(count) {
        var result = "",
            str = this.toString();
        for (var i = 0; i < count; i++) {
            result += str;
        return result;
    };
    
  9. repeat(count, max = 5, zero = '---')
    String.prototype.$repeat = function (count, max = 5, zero = '---') {
      if (count > max) {
        return `${this} x ${count}`;
      } else if (count > 0) {
        return this.repeat(count);
      } else {
        return zero;
    };
    ...