Javascript String repeat(count) method 3

Description

Javascript String repeat(count) method 3



String.prototype.repeat = function(count) {
    var result = "";

    var i;/*from w  w w  . java 2 s .  com*/
    for (i = 0; i < count; i += 1) {
        result += this;
    }

    return result;
}

console.log("Problem 4. Useful String Methods");
var example = "This is an example string used only for demonstration purposes.";
console.log(example.startsWith("This"));
console.log(example.startsWith("this"));
console.log(example.startsWith("other"));

console.log(example.endsWith("poses."));
console.log(example.endsWith("example"));
console.log(example.startsWith("something else"));

console.log(example.left(9));
console.log(example.left(90));

console.log(example.right(9));
console.log(example.right(90));

//Combinations must also work
example = "abcdefgh";
console.log(example.left(5)
                   .right(2));

var hello = "hello";
console.log(hello.padLeft(5));
console.log(hello.padLeft(10));
console.log(hello.padLeft(5, "."));
console.log(hello.padLeft(10, "."));
console.log(hello.padLeft(2, "."));

console.log(hello.padRight(5));
console.log(hello.padRight(10));
console.log(hello.padRight(5, "."));
console.log(hello.padRight(10, "."));
console.log(hello.padRight(2, "."));

var character = "*";
console.log(character.repeat(5));
// Alternative syntax
console.log("~".repeat(3));

// Another combination
console.log("*".repeat(5)
               .padLeft(10, "-")
               .padRight(15, "+"));

console.log("\n\n");

Javascript String repeat(count)

// String.prototype.repeat
String.prototype.repeat = function (count) {
  return new Array((count || 0) + 1).join(this);
};

Javascript String repeat(count)

String.prototype.repeat = function (count) {
    let accu = '';
    for (let i = 0; i < count; i++) {
        accu += this.toString();//from  w w  w  .  j a va 2s .co m
    }
    return accu;
};

Javascript String repeat(count)

//adding a repeat method to the String type
String.prototype.repeat = function (count) {
  var str,/*from w  ww  . ja v a  2s  .  c  o m*/
    pattern,
    i;
  pattern = String(this);
  if (!count) {
    return pattern;
  }
  str = '';
  for (i = 0; i < count; i += 1) {
    str += pattern;
  }
  return str;
};

console.log('-'.repeat(23));

Javascript String repeat(count)

String.prototype.repeat = function(count) {
  var res = "";
  while(count--) res += this;
  return res;//from  w w  w. j a  v a 2 s.  co  m
};

Javascript String repeat(count)

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 a v  a  2 s  .  c om
    return result + pattern;
};

Javascript String repeat(count)

String.prototype.repeat = function repeat(count) {
 'use strict';//from   w  w  w  .j  a  va2  s  .  c o  m

 if (this === undefined || this === null) {
  throw new TypeError(this + ' is not an object');
 }

 if (count < 0 || count === Infinity) {
  throw new RangeError(count + ' is less than zero or equal to infinity');
 }

 return new Array((parseInt(count, 10) || 0) + 1).join(this);
};

Javascript String repeat(count)

String.prototype.repeat = function(count) {
    var pattern = this,
        str = '';

    if(!count) {/*from  www .  ja  v a  2s  .  c  o  m*/
        return pattern;
    }

    for(var i = 0; i < count; i++) {
        str += pattern;
    }

    return str;
};

String.prototype.capitalize = function() {
    return this.toUpperCase();
};


console.log('wiga '.repeat(3));
console.log('wiga '.capitalize());

Javascript String repeat(count)

/* *******************/*from www .  j  a v a 2  s  . c om*/
 * String extension: repeat(count) -> String
 * This method will repeat this string for a specified count and return the result, leaving this string unchanged.
 */
String.prototype.repeat = function (count) {
 if (count < 1) return '';
 var str = '';
 while (count > 0) {
  str += this;
  count--;
 }
 return str;
}

Javascript String repeat(count)

'use strict';// www .java2 s .  c om

/**
 * Repeat a string
 * @param  {Number} count
 * @return {String}
 */
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;
};

Javascript String repeat(count)

"use strict";/*from   www  .  j  a  va 2s . c o m*/
//Strategy: start from the middle, and push new rows to end and shift to back
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;
  }
  return result + pattern;
};

function diamond(n){
  if(n % 2 === 0 || n < 0) return null; //Can't be even
  
  let diam = "";

  for(let i = 1; i <= n + 1; i+=2) {
    diam += " ".repeat((n-i)/2) + "*".repeat(i) + "\n";
  }

  for(let i = n - 2; i >= 1; i-=2) {
    diam += " ".repeat((n-i)/2) + "*".repeat(i) + "\n";
  }

  return diam;
}

console.log(diamond(7));

Javascript String repeat(count)

// 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   ww w  . jav  a2  s . c o m*/

// 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);
};



PreviousNext

Related